SpringMVC 上傳圖片保存到服務器 同時更改圖片名稱保存至數據庫



    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    public String saveDriver(
            @RequestParam(value = "photo", required = false) MultipartFile filedata,
            Driver driver, Model model) {

        trimDriver(driver);
        model.addAttribute(driver);
        // 判斷圖片大小是否大於2M
        if (filedata.getSize() > Constant.UPLOAD_PHOTO_SIZE) {
            model.addAttribute("isSaveSuccess", false);
            IMessage msg = new Message("MSG_YS030_002", MsgTypeEnum.INFO, null);
            model.addAttribute("msg", msg.toString());
            return "ys030/YS033";
        }
        // 判斷司機是否已存在
        if (this.ys030Service.isExist(driver.getOrganizationId(),
                driver.getIdCardNumber(), null)) {
            model.addAttribute("isSaveSuccess", false);
            IMessage msg = new Message("ERR_YS030_003", MsgTypeEnum.ERROR, null);
            model.addAttribute("msg", msg.toString());
            return "ys030/YS033";
        }
        // 保存相對路徑到數據庫 圖片寫入服務器
        if (filedata != null && !filedata.isEmpty()) {
            // 獲取圖片的文件名
            String fileName = filedata.getOriginalFilename();
            // 獲取圖片的擴展名
            String extensionName = fileName
                    .substring(fileName.lastIndexOf(".") + 1);
            // 新的圖片文件名 = 獲取時間戳+"."圖片擴展名
            String newFileName = String.valueOf(System.currentTimeMillis())
                    + "." + extensionName;

            try {
                driver.setPicUrl(newFileName);
                saveFile(newFileName, filedata);

            } catch (Exception e) {
                log.error("上傳圖片失敗.", e);
                model.addAttribute("isSaveSuccess", false);
                IMessage msg = new Message("MSG_YS030_001", MsgTypeEnum.INFO,
                        null);
                model.addAttribute("msg", msg.toString());
                return "ys030/YS033";
            }
        }
        this.ys030Service.save(driver);
        model.addAttribute(driver);
        model.addAttribute("isSaveSuccess", true);
        return "ys030/YS033";
    }

    private void saveFile(String newFileName, MultipartFile filedata) {
        // TODO Auto-generated method stub
        // 根據配置文件獲取服務器圖片存放路徑
        String picDir = "";
        try {
        //這里封裝了讀取配置文件的方法 配置文件中有圖片的存放地址和獲取地址
            Properties properties = PropertiesUtil
                    .getProperties("configure/driverpicurl.properties");
            picDir = properties.getProperty("savePicUrl");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String saveFilePath = picDir;

        /* 構建文件目錄 */
        File fileDir = new File(saveFilePath);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }

        try {
            FileOutputStream out = new FileOutputStream(saveFilePath + "\\"
                    + newFileName);
            // 寫入文件
            out.write(filedata.getBytes());
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void deleteFile(String oldPicName) {
        // TODO Auto-generated method stub
        String picDir = "";
        try {
            Properties properties = PropertiesUtil
                    .getProperties("configure/driverpicurl.properties");
            picDir = properties.getProperty("savePicUrl");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        /* 構建文件目錄 */
        File fileDir = new File(picDir+"/"+oldPicName);
        if (fileDir.exists()) {
            //把修改之前的圖片刪除 以免太多沒用的圖片占據空間
            fileDir.delete();
        }

    }

這是properties 文件
#============================================================================
#savePicUrl=D:/tomcat/apache-tomcat-7.0.39/webapps/driverPic 服務器圖片存放路徑(服務器絕對路徑與工程同目錄)
#getPicUrl=http://xxx.xxx.xx.xxx:8080/driverPic/ 服務器圖片獲取地址
#============================================================================
savePicUrl=D:/tomcat/apache-tomcat-7.0.39/webapps/driverPic
getPicUrl=http://xxx.xxx.xx.xxx:8080/driverPic/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM