Spring Boot接收前端上传的多个文件

Spring Boot接收前端上传的多个文件

突然被问到这个功能,太久没用到这个功能,印象有些模糊,这个文章记录一下.
前端那里会传一个字段和一些文件到后端,然后后端接收保存一下.

代码

可以直接使用 List<MultipartFile> 来接收多个文件,如果要传别的参数可以用对象接收

    @PostMapping(value = "uploadFiles")
    @ResponseBody
    public RespBean uploadFiles(@RequestParam(value = "file")List<MultipartFile> files, User user) {
        for (MultipartFile file : files) {
            System.out.println(file.getOriginalFilename());
        }
        System.out.println(user);
        return RespBean.ok("ok");
    }

或者添加 @RequestParam

    @PostMapping(value = "uploadFiles")
    @ResponseBody
    public RespBean uploadFiles(@RequestParam(value = "file")List<MultipartFile> files, @RequestParam(value = "id") String id) {
        for (MultipartFile file : files) {
            System.out.println(file.getOriginalFilename());
            System.out.println(id);
        }
        return RespBean.ok("ok");
    }

看之前的代码里在接收的对象前加了个 @RequestBody , 这个会导致报错

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;boundary=**;charset=UTF-8' not supported

stackoverflow 老哥的话 @RequestBody means use of JSON or XML data with maps your DTO bean.

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue-Spring Boot 项目中,可以使用 Vue.js 前端框架和 Spring Boot 后端框架来实现多文件上传。具体步骤如下: 1. 在 Vue.js 中,可以使用 `<input type="file" multiple>` 标签来实现多文件上传。 2. 在 Vue.js 中,可以使用 `FormData` 对象来构造包含文件数据的表单数据,然后使用 `axios.post()` 方法将表单数据发送到后端。 3. 在 Spring Boot 中,可以使用 Spring MVC 组件的 `@RequestParam("file") MultipartFile[] files` 参数来接收上传文件数组。 4. 在 Spring Boot 中,可以使用 `MultipartFile` 对象的 `transferTo()` 方法将上传文件保存到指定的目录中。 一个简单的 Vue-Spring Boot文件上传示例代码如下: Vue.js 前端代码: ```html <template> <div> <input type="file" multiple @change="onFileChange"> <button @click="uploadFiles">上传</button> </div> </template> <script> import axios from 'axios' export default { data() { return { files: null } }, methods: { onFileChange(event) { this.files = event.target.files }, uploadFiles() { const formData = new FormData() for (let i = 0; i < this.files.length; i++) { formData.append('file', this.files[i]) } axios.post('/upload', formData) .then(response => { console.log(response.data) }) .catch(error => { console.log(error) }) } } } </script> ``` Spring Boot 后端代码: ```java @PostMapping("/upload") public String uploadFiles(@RequestParam("file") MultipartFile[] files) { try { for (MultipartFile file : files) { String fileName = file.getOriginalFilename(); String filePath = "/path/to/save/file/" + fileName; file.transferTo(new File(filePath)); } return "上传成功"; } catch (Exception e) { e.printStackTrace(); return "上传失败"; } } ``` 需要注意的是,如果上传文件比较大,可能会导致内存溢出。可以考虑使用 `MultipartFile` 对象的 `transferTo()` 方法直接将文件保存到磁盘上,避免将文件保存在内存中。另外,在实际开发中,还需要考虑安全性和性能优化等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值