Spring Cloud 升级 Spring Boot 从 1.5.x 版本 至 2.1.x 版本,服务消费方带参数文件上传报错排查处理!

有段时间没有更新博客的内容了!

今天刚刚好对Spring Cloud 升级 Spring Boot 从 1.5.x 版本 至 2.1.x 版本,服务消费方带参数文件上传接口报错,报错内容如下!

the request was rejected because no multipart boundary was found

org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

此错误是通过 Feign 调用接口时报错!Feign 如下!

@RequestMapping(value = "/handler", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public RestResult handleFileUpload(@RequestPart(value = "file") MultipartFile file,
			@RequestParam("hostId") long hostId, @RequestParam("labelId") long labelId,
			@RequestParam("basePath") String basePath);

服务消费方如下

@RequestMapping(value = "/handler", method = RequestMethod.POST)
	public RestResult handleFileUpload(@RequestPart(value = "file") MultipartFile file,
			@RequestParam("hostId") long hostId, @RequestParam("labelId") long labelId,
			@RequestParam("basePath") String basePath) throws IOException {
		log.debug(file.getOriginalFilename());
		log.debug("" + hostId);
		log.debug("" + labelId);
		log.debug(basePath);
		HRepertory hRepertory = uploadServiceImpl.uploadAndInsertHRepertory(file, hostId, labelId, basePath);
		if (hRepertory == null) {
			return RestResult.noData();
		}
		logger.info(hRepertory.toString());
		return RestResult.ok(hRepertory);
	}

在之前的 1.5.x 版本中没有问题,升级版本后报错!百度搜索,有提到过 Content-Type 不要定义便可解决此问题!

在 Feign 中已经定义了 consumes = MediaType.MULTIPART_FORM_DATA_VALUE 这个部分!

不定义 consumes 属性会导致服务提供方不知道如何序列化参数!

经过反复测试实验,最终通过修改 consumes 属性值上增加一个 + ";" 来最终实现文件、参数均可上传!

修改后的 Feign 如下

@RequestMapping(value = "/handler", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE
			+ ";")
	public RestResult handleFileUpload(@RequestPart(value = "file") MultipartFile file,
			@RequestParam("hostId") long hostId, @RequestParam("labelId") long labelId,
			@RequestParam("basePath") String basePath);

最终查看服务消费方获取请求时的 Content-Type :

到这里应该知道问题的缘由了

在Feign的 MultipartFormContentProcessor 源码中,其中有一段就专门是用来添加boundary

如果小伙伴有更好的解决方案,请君下方留言!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值