问题描述:

	@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. scp 和 rsync

    scp 和 rsync指令的区别 相同点两者都可以被用来进行数据同步 不同点 : 对于scp来讲 是全量复制 以当前主机为准 将相同的文件拷贝到另一台机器上 rsync 可以识别增量的内容,可以仅仅对 ...

  2. mac使用expect登录跳板机后的机器

    两个文档 #!/usr/bin/expect -f #连接文件名字记录 set ip [lindex $argv 0] catch {spawn ssh 1.1.1.1}## ip地址换成自己的 ex ...

  3. cython并行性能-计算滚动求和 rolling function

    cython通过编译为C程序提高性能有很多例子,通过OpenMP并行的性能没那么多. 今天尝试了一下似乎gcc对parallelism reduction优化的很厉害,加上OpenMP并行可以提高20 ...

  4. python 成功解决import librosa出错问题

    在做音频处理时,用到了librosa这个库,但是一直在报错,一开始以为代码错误,后来发现import的时候就已经出错了. 我给他卸载了重新安装,结果是一样的,报错如下: Traceback (most ...

  5. 小程序隐藏scroll-view滚动条的方法

    在wxss文件上加上 ::-webkit-scrollbar{ width: 0; height: 0; color: transparent; }

  6. C++和C中的输入输出总结、标准输入/标准输出/标准错误与重定向,>>、>、<、<<

    标准输入/标准输出/标准错误与重定向 0表示标准输入.1表示标准输出.2标准错误.1和2都是默认是输出到屏幕. linux中的>>.>.<.<<:这些符号是Linu ...

  7. 无感知WPF窗口透明加穿透

    在窗口的XAML文件中添加以下属性: ShowInTaskbar="False" WindowStyle="None" AllowsTransparency=& ...

  8. vue获取标签对象的方式

    我知道2种方式: 1.在标签内 使用 ref 属性定义对象名,使用this.$refs.[name] 调用 2.在标签内 使用 函数传递事件对象, 定义, <div @click="h ...

  9. 51电子-STC89C51开发板:目录

    51电子(我要电子:www.51dz.com),是国内最早一批的电子类相关网站,在深圳有实体店. 这个系列文章以 STC89C51 来做笔记,讲解使用过程. --------------------- ...

  10. Python 使用mysql.connector、pymysql和 MYSQLdb(MysqlClient)操作MySQL数据库

    MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一.本文主要介绍安装mysql.connector,. ...