• 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/

  • 相关阅读:
    学习总结之三(SQL SERVER游标CURSOR的使用)
    根据经纬度坐标计算两点间几何距离 椰子树下 CSDN博客
    多个CSS风格共用同一(背景)图片_那一片天_百度空间
    JAVA md5把我气到疯的代码,天哪,神呀,我的C# 啊。
    SQL中的系统变量一览
    在Windows下安装Redmine
    Fragment中的方法findFragmentById(int id)的返回值探讨
    AndroidVideoCache 框架源码分析
    从零开始编译属于你的FFmpeg
    Glide填坑指南
  • 原文地址:https://www.cnblogs.com/baojiao/p/6711034.html
Copyright © 2020-2023  润新知