Required request part 'file' is not present
问题描述:
@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的更多相关文章
- HTTP Status 400 - Required request part 'file' is not present
今天使用Spring MVC做一个文件上传的功能,在提交表单的时候出现了如下错误:
- Required MultipartFile parameter 'file' is not present error
<input type=“file”> 中的name 与id 属性 与 addbanner(@RequestParam("file") MultipartFile ...
- Spring MVC文件上传出现错误:Required MultipartFile parameter 'file' is not present
1.配置文件上传的解析器 首先需要在spring mvc的配置文件中(注意是spring mvc的配置文件而不是spring的配置文件:applicationContext.xml)配置: sprin ...
- 错误:Required request parameter 'XXX' for method parameter type String is not present
错误信息:Required request parameter 'XXX' for method parameter type String is not present 这种都是前端请求方式不同,后 ...
- HTTP Status 400 - Required String parameter 'userName' is not present 错误
HTTP Status 400 - Required String parameter 'userName' is not present 错误 先mark 有时间详细写 参考链接: https:/ ...
- required string parameter XXX is not present
@RequestParam jQuery调用方式: deleteFile: function(filePath) { return ajax({ method: 'POST', url: '/cm/s ...
- @RequestBody对象为空,异常Required request body is missing
1.异常 org.springframework.http.converter.HttpMessageNotReadableException: Required request body is mi ...
- [已解决]报错:Required request body is missing
问题代码: res = requests.post(getXxxxList_url, headers=headers, data={}) 对象网站: angular4 apache 通过验证 (coo ...
- 前端传送JSON数据,报Required request body is missing
声明: 后端为Java,采用SSM框架 前端一个JSON.stringify()传来的json字符串,后端一般用@RequestBody标签来定义一个参数接收 但问题在于,当我使用get方式传JSON ...
- 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 ...
随机推荐
- chrome驱动版本与python不一致时
图片: 解决办法: 1.先去chrome浏览器找到版本号:在chrome找到帮助-关于Google chrome 2.通过网址:https://npm.taobao.org/mirrors/chrom ...
- nnlog 日志模块
python自带的logging模块理解起来比较费劲,直接pip install nnlog即可 1 import nnlog 2 log = nnlog.Logger(file_name='my.l ...
- P12证书转BKS证书
安卓 识别的证书格式是bks ,而我之前生成的证书格式是p12 所以需要转换一下,至于怎么生成p12,请看我转载的的文章 Nginx https 双向认证. 1.请先下载第三方转换工具protecl ...
- ubuntu 20.04使用kubeadm安装k8s集群
本文主要用于记录,步骤参考了:https://blog.csdn.net/weixin_44559544/article/details/123381441 一.设备相关准备 1.修改节点主机名,这样 ...
- Access 时间查询 与 SQL SERVER 不一样的几个地方
最近用到了ACCESS 数据库 ,与之前用SQL SERVER 还是有点区别的. 1. 时间 之间 用 # 连接 如下: public static string GetUserInfo(stri ...
- 设置cudnn日志
主要分为两个步骤,分别设置两个环境变量. 第一步,设置日志级别. # 三种级别,设置环境变量为1为开启,设置为0关闭. # CUDNN_LOGINFO_DBG # CUDNN_LOGWARN_DBG ...
- js原型,继承
javascript是为了成为轻量级的语言而开发的.开发者并没有选择像其他面向对象的语言一样,定义类(class),而是使用了protopype实现继承. 每一个函数在创建时,都会创建它的Protot ...
- 编译内核出现错误cc1: error: code model kernel does not support PIC mode
删除该模块目录下的 .cache.mk 文件就好了,重新 make 即可
- WSL2 网络异常排查 [ping 不通、网络地址异常、缺少默认路由、被宿主机防火墙拦截]
最近在使用的 wsl2 的时候突然发现 wsl2 无法正常联网,即 ping 不通外网以及宿主机的 wsl 网卡.但是将 wsl 版本设置为 1 就可以联网了. 如果你是正常使用的时候,并且自己没有手 ...
- span服务器控件
Label Button 这些控件在服务器端都有对应的类可以去实例化一个对象, 可是像 span 这些在服务器没有对应的类可实例的, 在服务器端要实例化一个 span 控件时怎么办呢? 可以使用 Ht ...