问题描述:

	@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后就不会出现,直接这样使用没问题

最新文章

  1. Jexus web server V5.4.5 已经发布
  2. [第三方]SCNetworkReachability 获取网络状态控件使用方法
  3. Java基础之扩展GUI——使用对话框创建文本元素(Sketcher 4 creating text elements)
  4. unity3D里面的点乘和叉乘
  5. PHP无限极分类实现
  6. Java基础(34):Java中基本数据类型的包装类(主要为了不同数据类型之间更方便的进行转换)(Wrapper类)
  7. View页面根据权限显示不同的内容
  8. HBase 系统架构
  9. go学习资料及优秀博文
  10. 面向站长和网站管理员的Web缓存加速指南
  11. gitolite随记
  12. 201521123081《Java程序设计》 第10周学习总结
  13. C# 串口接收数据中serialPort.close()死锁
  14. 蓝桥杯练习系统—基础练习 2n皇后问题
  15. JAVA_SE基础——55.自定义异常类
  16. Xcode7.2中如何添加一个Empty Application模板
  17. FFmpeg and x264 Encoding Guide
  18. VMware下对Ubuntu进行扩充磁盘大小
  19. hiho 第六周 01背包
  20. 获取select被选中的option的值

热门文章

  1. jumpserver运行源码
  2. sys&faker&jsonpath模块、异常处理、多线程、多进程
  3. 如何用python将txt中的package批量安装
  4. 改变Jupyter notebook默认浏览器
  5. 基于Nginx上的docker负载均衡
  6. 关于window的文件路径
  7. django 安装cerlery error in anyjson setup command: use_2to3 is invalid.
  8. 使用JFinal实现简单的学生管理系统
  9. 2021 icpc 沈阳 I 【分式线性变换的保交比性】
  10. mac上gitclone出现password: Permission denied, please try again.