SpringBoot实现多文件上传

时间:2020-03-30 14:35:12   收藏:0   阅读:503

 

    @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

 

作者:李永明

原文:https://www.cnblogs.com/it-deepinmind/p/12598286.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!