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踩坑

参考

巨坑

posted @ 2021-07-02 19:05  johnny233  阅读(436)  评论(0编辑  收藏  举报  来源