springboot 2.7 文件上传采坑Required request part ‘file‘ is not present

新搭建了一个springboot 框架项目,调试完使用的过程,写文件上传接口,发现一直报错:

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public String handleFileUpload(@RequestParam("files") List<MultipartFile> files) {
        // Process the uploaded files
        for (MultipartFile file : files) {
            // You can access each file's content using file.getInputStream(), file.getBytes(), etc.
            // Process the files as needed.
        }
        return "Upload successful";
    }
}

采坑1:

Required request part 'fileList' is not present RequestPartMethodArgumentResolver.resolveArgument

网上各种原因,最终发现是springboot 2.7版本针对

spring.mvc.hiddenmethod.filter.enabled=true配置,默认为false, 在springboot的旧版本中,默认是true.

解决:

在application.properties文件中,增加该配置即可

采坑2:

springboot 多文件上传异常:MultipartFile resource [fileList] cannot be resolved to URL

分析:
由于是采用form-data 格式:multipart/form-data,初步分析是 springboot 在接收到fileList时,form表单的格式无法解析为json序列化,具体还待后面细分析

初步解决思路:

本意是多文件批量上传,如此不行,则继续单文件上传,转换思路是把多文件压缩为zip文件,然后服务端接收到后,再行解压,即可

先附上chatgpt 生成的解压文件代码,后面验证:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

@RestController
public class ZipController {

    @PostMapping("/unzip")
    public String unzip(@RequestParam("zipFile") MultipartFile zipFile) {
        try {
            File destDir = new File("unzipped");
            if (!destDir.exists()) {
                destDir.mkdirs();
            }

            try (ZipInputStream zipInputStream = new ZipInputStream(zipFile.getInputStream())) {
                ZipEntry zipEntry;
                while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                    String entryName = zipEntry.getName();
                    File entryFile = new File(destDir, entryName);

                    if (entryFile.getParentFile() != null) {
                        entryFile.getParentFile().mkdirs();
                    }

                    if (!zipEntry.isDirectory()) {
                        try (FileOutputStream fos = new FileOutputStream(entryFile)) {
                            byte[] buffer = new byte[1024];
                            int bytesRead;
                            while ((bytesRead = zipInputStream.read(buffer)) != -1) {
                                fos.write(buffer, 0, bytesRead);
                            }
                        }
                    }
                }
            }

            // 遍历解压后的文件
            for (File file : destDir.listFiles()) {
                if (file.isFile()) {
                    System.out.println("File: " + file.getName());
                } else if (file.isDirectory()) {
                    System.out.println("Directory: " + file.getName());
                }
            }

            return "ZIP文件解压缩成功并遍历完成";
        } catch (IOException e) {
            e.printStackTrace();
            return "ZIP文件解压缩失败";
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 当出现"Required request part 'file' is not present"错误时,通常是由于在文件上传接口中没有正确传递文件参数导致的。\[1\]这个错误信息表明请求中缺少名为'file'的必需文件参数。\[2\]在使用Postman测试时,确保在请求中正确设置了文件参数,并将其命名为'file'。\[2\]另外,还要确保请求的Content-Type设置为"multipart/form-data",以支持文件上传。如果仍然遇到问题,可以检查后台日志,查看是否有类似于"MissingServletRequestPartException"的警告信息,以获取更多的错误详情。\[3\] #### 引用[.reference_title] - *1* [springboot文件上传报错:Required request part ‘file‘ is not present](https://blog.csdn.net/Tianguoping/article/details/120507284)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Springboot文件上传接口,一直报Required request part ‘zipFile‘ is not present的错误](https://blog.csdn.net/ylx1066863710/article/details/120652555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [antd3 vue3 springboot 上传文件 解决Required request part ‘file‘ is not present](https://blog.csdn.net/fly19920602/article/details/126328535)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值