• 上传照片到本地服务器上(亲测有效)


    public class UploadImgUtils {
    
        private static String savePath = "";
    
        /**
         * 上传照片工具类
         *
         * @param file     文件
         * @param workNo   工单号
         * @param property 配置的环境(dev,prod,test)
         * @return
         * @throws OperationException
         */
        public static String uploadImg(MultipartFile file, String workNo, String property) throws OperationException {
            if (file == null) {
              System.out.println("异常"); 
            }
            if (file.getSize() > 1024 * 1024 * 1) {
              System.out.println("异常");
            }
            //获取文件后缀
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
            if (!"jpg,jpeg,gif,png".toUpperCase().contains(suffix.toUpperCase())) {
                System.out.println("异常");
            }
            //对savePath进行过赋值
            getProperties(property);
            File savePathFile = new File(savePath);
            if (!savePathFile.exists()) {
                //若不存在该目录,则创建目录
                savePathFile.mkdir();
            }
            //用工单号作为唯一的标识符
            String filename = workNo + "." + suffix;
            try {
                //将文件保存指定目录
                file.transferTo(new File(savePath + filename));
            } catch (Exception e) {
               System.out.println("异常");
            }
            //返回文件名称
            return savePath + filename;
        }
    
        /**
         * 读取配置文件中的信息.
         *
         * @return
         */
        private static void getProperties(String name) {
            YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
            factoryBean.setResources(new ClassPathResource("application-" + name + ".yml"));
            factoryBean.afterPropertiesSet();
            Properties object = factoryBean.getObject();
    在配置文件中书写配置路径信息 savePath
    = (String) object.get("savePath"); } }

    上面的这个是工具类,需要在配置文件中配置上传的路径信息

  • 相关阅读:
    路飞学城Python-Day19
    路飞学城Python-Day18
    路飞学城Python-Day17
    ES6新特性概览
    关于Flex,有12个属性很重要
    Web前端工程师成长之路——知识汇总
    解决ajax跨域请求 (总结)
    项目中关于AJAX的使用总结
    Web前端性能优化——如何提高页面加载速度
    Canvas和SVG的区别
  • 原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/12128408.html
Copyright © 2020-2023  润新知