问题描述:

	@RequestMapping(value = "upload", method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void upload(@RequestPart MultipartFile upfile) throws Exception {
return fileStorageService.upload(upfile);
}

上面的代码是上传一个文件到upload方法中,然后在该方法中调用fileStorageService(FeignClient)去上传

fileStorageService.upload方法如下:

@FeignClient(name = "STORAGE")
@RequestMapping("/file")
public interface FileStorageService {
@PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String upload(@RequestPart MultipartFile file);
}

在实际运行过程中,报错:

org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present

问题原因

因为upfile的属性是upfile,而不是file,传递到fileStorageService.upload方法中的时候,找不到file标记的文件内容

解决方案

方案一:让两个方法的参数名一致

方案二:处理upfile对象,让它生成一个新的MutipartFile对象

MultipartFile multipartFile = new MultipartFileDto(PlatformStorage.MinioMutipartFileFieldName.FIELD_NAME,  bytes);
//自定义一个MultipartFile
public class MultipartFileDto implements MultipartFile {
private final String name; private String originalFilename; private String contentType; private final byte[] content; /**
* Create a new MultipartFileDto with the given content.
* @param name the name of the file
* @param content the content of the file
*/
public MultipartFileDto(String name, byte[] content) {
this(name, "", null, content);
} /**
* Create a new MultipartFileDto with the given content.
* @param name the name of the file
* @param contentStream the content of the file as stream
* @throws IOException if reading from the stream failed
*/
public MultipartFileDto(String name, InputStream contentStream) throws IOException {
this(name, "", null, FileCopyUtils.copyToByteArray(contentStream));
} /**
* Create a new MultipartFileDto with the given content.
* @param name the name of the file
* @param originalFilename the original filename (as on the client's machine)
* @param contentType the content type (if known)
* @param content the content of the file
*/
public MultipartFileDto(String name, String originalFilename, String contentType, byte[] content) {
this.name = name;
this.originalFilename = (originalFilename != null ? originalFilename : "");
this.contentType = contentType;
this.content = (content != null ? content : new byte[0]);
} /**
* Create a new MultipartFileDto with the given content.
* @param name the name of the file
* @param originalFilename the original filename (as on the client's machine)
* @param contentType the content type (if known)
* @param contentStream the content of the file as stream
* @throws IOException if reading from the stream failed
*/
public MultipartFileDto(String name, String originalFilename, String contentType, InputStream contentStream)
throws IOException { this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
} @Override
public String getName() {
return this.name;
} @Override
public String getOriginalFilename() {
return this.originalFilename;
} @Override
public String getContentType() {
return this.contentType;
} @Override
public boolean isEmpty() {
return (this.content.length == 0);
} @Override
public long getSize() {
return this.content.length;
} @Override
public byte[] getBytes() throws IOException {
return this.content;
} @Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(this.content);
} @Override
public void transferTo(File dest) throws IOException, IllegalStateException {
FileCopyUtils.copy(this.content, dest);
} }

方案三:升级springboot和springcloud

我发现这个问题在2.1.7.RELEASE和Greenwich.SR2下会出现,但是升级到2.4.0和2020.0.4后就不会出现,直接这样使用没问题

Required request part 'file' is not present的更多相关文章

  1. HTTP Status 400 - Required request part 'file' is not present

    今天使用Spring MVC做一个文件上传的功能,在提交表单的时候出现了如下错误:

  2. Required MultipartFile parameter 'file' is not present error

    <input type=“file”>  中的name 与id 属性 与  addbanner(@RequestParam("file") MultipartFile ...

  3. Spring MVC文件上传出现错误:Required MultipartFile parameter 'file' is not present

    1.配置文件上传的解析器 首先需要在spring mvc的配置文件中(注意是spring mvc的配置文件而不是spring的配置文件:applicationContext.xml)配置: sprin ...

  4. 错误:Required request parameter 'XXX' for method parameter type String is not present

    错误信息:Required request parameter 'XXX' for method parameter type String is not present 这种都是前端请求方式不同,后 ...

  5. HTTP Status 400 - Required String parameter 'userName' is not present 错误

    HTTP Status 400 - Required String parameter 'userName' is not present 错误 先mark  有时间详细写 参考链接: https:/ ...

  6. required string parameter XXX is not present

    @RequestParam jQuery调用方式: deleteFile: function(filePath) { return ajax({ method: 'POST', url: '/cm/s ...

  7. @RequestBody对象为空,异常Required request body is missing

    1.异常 org.springframework.http.converter.HttpMessageNotReadableException: Required request body is mi ...

  8. [已解决]报错:Required request body is missing

    问题代码: res = requests.post(getXxxxList_url, headers=headers, data={}) 对象网站: angular4 apache 通过验证 (coo ...

  9. 前端传送JSON数据,报Required request body is missing

    声明: 后端为Java,采用SSM框架 前端一个JSON.stringify()传来的json字符串,后端一般用@RequestBody标签来定义一个参数接收 但问题在于,当我使用get方式传JSON ...

  10. Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public xxxxxxxx.

    最近在使用 springBoot开发的时候, 使用PostMan访问接口,  返回一个 404 ,  后台报一个 warn : Failed to read HTTP message: org.spr ...

随机推荐

  1. 【SSO单点系列】(7):CAS4.0 二级域名

    CAS4.0 二级域名 一.描述 当cas成功登录后如果访问同一域名下的资源是 被当作同一应用下资源不需要再次请求登录,但是如果二级域名不同会 被当作不同应用在访问 需要请求CAS 在请求时会把TGC ...

  2. 「postOI」Colouring Game

    题意 有 \(n\) 个格子排成一行,一开始每个格子上涂了蓝色或红色. Alice 和 Bob 用这些格子做游戏.Alice 先手,两人轮流操作: Alice 操作时,选择两个相邻的格子,其中至少要有 ...

  3. 传统编码方式转 gRPC 注意事项

    # 赋值编码: 1.pbBuilder 设置值时不能为 null 2.pb 定义的类是不可变类,赋值时需要使用 Builder 模式,且每次 builder 都会 new 一个新对象,所以赋值时需要特 ...

  4. python常用的六个字符串处理方法

    1.upper(将小写字母转换为大写) 语法: str.upper() 参数: NA 实例: a = 'abc' b = a.upper() print(b) 结果: ABC 2.lower(将小写字 ...

  5. redis的windows下安装

    转载https://www.runoob.com/redis/redis-install.html Redis 安装 Windows 下安装 下载地址:https://github.com/tpora ...

  6. moduleNotFoundError:No module named 'exceptions'

    如果pip install docx 过请先卸载,输入如下指令: pip uninstall docx 方法一: pip install python-docx 方法二: 下载: python_doc ...

  7. SQL CASE 标注

    根据 状态值 显示中文备注 case when a.zht='0' then '录入' when  a.zht='1' then '待审核' when a.zht='2' then '已审核' end ...

  8. ApexSQLDBA 2019.02.1245[破解补丁]

    ApexSQL DBA 2019.02.1245 破解补丁 支持ApexSQL Log.ApexSQL Plan.ApexSQL Recover 该版本支持SQLSERVER 2019 开源地址: h ...

  9. Delphi as 和 is 的使用

    as就是把某个类型对象转换为某个指定类型,这样方便使用指定类型中的一些成员. is就是判断某个对象是不是某个类型,可以筛选出我们需要的对象. 下面是一个as is 的实例代码,该代码同时也实现了两种方 ...

  10. firefox 安装旧版flash播放器

    国内恶心的特供版flash用是不可能在用了,用旧版的火狐和旧版的flash播放器,亲测可用. 下载旧版本的火狐浏览器67.04 https://ftp.mozilla.org/pub/firefox/ ...