问题描述:

	@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. springboot上传图片

    springboot上传图片 新建一个springboot项目: 在java/main/com/ljx 创建一个controller.fileController类 内容如下: package com ...

  2. 借助mapshaper的简化来修复geojson的拓扑错误

    在使用turf.union方法合并面的时候,报错了.无法全部合并完成. 按照提示说是出现线的拓扑错误.至于哪里错误也看不出来. turf不提供拓扑修正功能的js包 难道有转到arcgis中去修复?好搓 ...

  3. 「postOI」以另一种方式证明 FWT

    记号 \(\otimes\) 代表或/与/异或卷积: \(\oplus\) 代表"拼接",例如 \(A\oplus B\) 即将 \(B\) 接在 \(A\) 的后面: \(+,- ...

  4. #pragma directive

    #pragma package(smart_init) #pragma package(smart_init)确保已打包的单元按照其依赖关系确定的顺序进行初始化(默认情况下包含在package(包)源 ...

  5. vue3 门户网站搭建5-图标

    奈何 element 自带的图标太少,不够用,故打算使用 vite-plugin-svg-icons 组件来封装 svg-icon . ps: ui 框架选用的 element-ui,为了能跟 vue ...

  6. Redis实战(三)Redis冷备如何做

    Redis 的 RDB 持久化方案,相信大家都有所了解,但是对于企业来说,如果只是持久化了一个 RDB 文件,不足以应付生产级别的事故.通常的方案就是对 RDB 进行多个备份,今天带大家来真枪实弹操作 ...

  7. autohotkey switching within applications

    class QdirManager { ppid := -1 ppath := "" __New(pathIn) { this.ppath := pathIn } __Delete ...

  8. EMQX+阿里云飞天洛神云网络 NLB:MQTT 消息亿级并发、千万级吞吐性能达成

    随着物联网技术的发展与各行业数字化进程的推进,全球物联网设备连接规模与日俱增.一个可靠高效的物联网系统需要具备高并发.大吞吐.低时延的数据处理能力,支撑海量物联网数据的接入与分析,从而进一步挖掘数据价 ...

  9. python调用包中函数__init__作用

    python调用包中函数__init__作用 lx:分享一个小知识点 lx:主函数中 调用了 包(目录)中某个模块的函数,但是没有出现模块名.函数铭 lx:可能是再包中的__init__中已经提前调用 ...

  10. C CODE STYLE 每天瞅一两眼

    阅读进度:只读到了swich 转载于:https://users.ece.cmu.edu/~eno/coding/CCodingStandard.html C Coding Standard C Co ...