zoukankan      html  css  js  c++  java
  • SpringBoot实现多文件上传

        @Value("${youku1327.file.root.path}")
        private String fileRootPath;
    
        @PostMapping(value = "/file/upload", produces = MediaType.MULTIPART_FORM_DATA_VALUE)
        public String fileUpload(@RequestParam("files") MultipartFile[] files){
            String filePath = "";
            // 多文件上传
            for (MultipartFile file : files){
                // 上传简单文件名
                String originalFilename = file.getOriginalFilename();
                // 存储路径
                filePath = new StringBuilder(fileRootPath)
                        .append(System.currentTimeMillis())
                        .append(originalFilename)
                        .toString();
                try {
                    // 保存文件
                    file.transferTo(new File(filePath));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return filePath;
        }

        @Value("${youku1327.file.root.path}")
        private String fileRootPath;
    
        @PostMapping(value = "/file/upload", headers = "content-type=multipart/form-data")
        public String fileUpload(@RequestParam("files") MultipartFile[] files){
            String filePath = "";
            // 多文件上传
            for (MultipartFile file : files){
                // 上传简单文件名
                String originalFilename = file.getOriginalFilename();
                // 存储路径
                filePath = new StringBuilder(fileRootPath)
                        .append(System.currentTimeMillis())
                        .append(originalFilename)
                        .toString();
                try {
                    // 保存文件
                    file.transferTo(new File(filePath));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return filePath;
        }

        @Value("${youku1327.file.root.path}")
        private String fileRootPath;
    
        @PostMapping(value = "/file/upload", headers = "content-type=multipart/form-data", produces = MediaType.MULTIPART_FORM_DATA_VALUE)
        public String fileUpload(@RequestParam("files") MultipartFile[] files){
            String filePath = "";
            // 多文件上传
            for (MultipartFile file : files){
                // 上传简单文件名
                String originalFilename = file.getOriginalFilename();
                // 存储路径
                filePath = new StringBuilder(fileRootPath)
                        .append(System.currentTimeMillis())
                        .append(originalFilename)
                        .toString();
                try {
                    // 保存文件
                    file.transferTo(new File(filePath));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return filePath;
        }

     

     

     最终用postman测试成功了。

    以下两个属性可以不需要:

    headers = "content-type=multipart/form-data", produces = MediaType.MULTIPART_FORM_DATA_VALUE

    作者:李永明

  • 相关阅读:
    设计模式之三:Abstract Factory(转)
    设计模式之二:adapter模式(转)
    设计模式之一:设计原则(转)
    双链表操作
    单链表操作
    C#-Activex插件操作指南
    积分源码上线
    換友情鏈接
    企业短信群发
    掉了,全掉了。
  • 原文地址:https://www.cnblogs.com/it-deepinmind/p/12598286.html
Copyright © 2011-2022 走看看