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 ...
随机推荐
- Nextcloud报 PHP zip 模块未安装
wget https://pecl.php.net/get/zip自动下载最新包 tar xf zip-1.20.0.tgz cd zip-1.20.0/ phpize 报错system libzip ...
- [洛谷/题目] P1562 还是N皇后
声明 关于科学道理都会放进代码中,但是我们需要先了解一下位运算解这道题目的基础知识 我不是很会专业词语,所以仅介绍原理 位运算基础 众所周知,二进制是0和1 2^3 2^2 2^1 2^0 8 4 2 ...
- [Swift]Swift图片显示方式设置,控件UIImageView的contentMode属性设置
contentMode属性是用来设置图片在UIImageView中的显示方式,如:拉伸.居中.填充等. 这里讨论的是UIImageView宽高固定,图片宽高不确定的情况.如社交APP的相册缩略图.手机 ...
- IEEE 802.66( WiMax)的衰亡
1.什么是WiMax WiMAX全称为,World Interoperability for Microwave Access,即全球微波接入互操作性,是一项基于IEEE 802.16标准的宽带无线接 ...
- ES 基础操作
集群 健康值的三种状态 Green:所有索引的所有分片均可用 primary 和 replice 均可用. Yellow 至少有一个 replice不可以用, 但是所有的 primary 正常. Re ...
- 安装kubernetes dashboard以及用户授权
kubernetes 版本v1.25.3 1.安装 版本查看:https://github.com/kubernetes/dashboard/releases kubectl apply -f htt ...
- 【服务器数据恢复】HP EVA存储多块硬盘离线的数据恢复案例
服务器故障&检测&分析:某品牌EVA存储设备中的RAID5磁盘有两块硬盘掉线,lun丢失.硬件工程师对故障服务器进行物理故障检测,发现掉线硬盘能够正常读取,无物理故障,也没有发现坏道. ...
- MySql索引底层原理(01)
目的:通过mysql获取数据,检索数据的原理来理解索引,以及如何利用好索引. 由于篇幅问题,可能会连载几篇文章. 从mysql获取一条数据说起: 我们知道,电脑的系统在获取数据的时候会旋转磁盘,然后移 ...
- debian最小化安装+sway记录
1. 最小化安装系统,只安装最基础的系统,如果是虚拟机中安装,安装SSH服务器可能更方便在宿主机终端操作客户机.deiban的安装器里有提供基础工具包的安装项,如果为了系统纯净不安装,可能会导致更多不 ...
- Oracle EBS 查看关联关系
打比方说,如何知道这个视图/表/包/trigger 在其他包/视图等引用. 1. select * from all_dependencies a where a.REFERENCED_NAME = ...