FileUploadException: the request was rejected because no multipart boundary was found

概述

最近在做一个视频检测的需要,使用Postman上传视频时,代码抛错:

ERROR 13557 [] [http-nio-5000-exec-8] org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found] with root cause

org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
        at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.init(FileItemIteratorImpl.java:178)

背景

业务应用层的文件上传Controller:

@Resource
private FileService fileService;

@PostMapping(value = "video")
@ApiOperation(value = "提交视频识别", httpMethod = "POST")
public Result video(@RequestParam("file") MultipartFile file, @LoginUser SysUser user, HttpServletRequest request) {
	Result<FileInfo> fileInfoResult = fileService.fileUpload(ArmConstant.BUCKET, file);
}

上面代码里面的FileService是基于FeignClient而定义的接口:

@FeignClient(name = ServiceNameConstants.FILE_SERVICE, fallbackFactory = FileServiceFallbackFactory.class, decode404 = true)
public interface FileService {
	@PostMapping(value = "/upload/{bucket}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	Result<FileInfo> fileUpload(@PathVariable("bucket") String bucket, @RequestParam("file") MultipartFile file);
}

FileService需要有一个支撑的服务,即file-center服务,其Controller定义如下:

@RestController
@Slf4j
public class FileController {
	@PostMapping("/upload/{bucket}")
	public Result<FileInfo> fileUpload(@PathVariable("bucket") String bucket, @RequestParam("file") MultipartFile file) {
	}
}

分析

全网百度以及Google搜索,几户全部都在讲如何设置Postman,一度让我质疑Postman的问题,或者是我使用Postman的问题。网上搜索时,一定要带着自己的批判思维分析,狠多文章都是瞎搬来乱抄去。再者,作为一款成熟的自动化接口测试工具,Postman几乎不会被你我发现bug。

但是,Postman的使用我可是滚瓜烂熟。且事实上,Postman配置也没任何问题:
在这里插入图片描述
搁置一晚上。

实在无法,全局搜索MultipartFile file
在这里插入图片描述
突然发现有点东西,@RequestPart和@RequestParam有何区别?
此时正好发现巨坑

把三个地方的@RequestParam替换成@RequestPart解决问题;

不得不说,Feign这玩意的坑,真不少。但是在Feign-GitHub搜不到FileUploadException: the request was rejected because no multipart boundary was found任何相关信息。

Feign的坑,参考我的另一篇blogFallbackFactory使用

延伸

@RequestPart和@RequestParam有何区别?
另写一篇@RequestParam和@RequestPart区别及Feign踩坑

参考

巨坑

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
FileUploadServlet.class package test; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class FileUploadServlet extends HttpServlet { // 定义文件的上传路径 private String uploadPath = "G://upload/"; // 限制文件的上传大小 private int maxPostSize = 100 * 1024 * 1024; public FileUploadServlet() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 得到用户需要保存的服装的id //String dressId = request.getParameter("dressID"); //System.out.println(dressId); // 保存文件到服务器中 response.setContentType("text/html; charset=UTF-8"); DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(4096); ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(maxPostSize); try { List fileItems = upload.parseRequest(request); Iterator iter = fileItems.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (!item.isFormField()) { String name = item.getName(); System.out.println(name); try { item.write(new File(uploadPath + name)); } catch (Exception e) { e.printStackTrace(); } } } } catch (FileUploadException e) { e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void init() throws ServletException { // Put your code here } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

johnny233

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值