问题描述:

  1. @RequestMapping(value = "upload", method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  2. public void upload(@RequestPart MultipartFile upfile) throws Exception {
  3. return fileStorageService.upload(upfile);
  4. }

上面的代码是上传一个文件到upload方法中,然后在该方法中调用fileStorageService(FeignClient)去上传

fileStorageService.upload方法如下:

  1. @FeignClient(name = "STORAGE")
  2. @RequestMapping("/file")
  3. public interface FileStorageService {
  4. @PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  5. String upload(@RequestPart MultipartFile file);
  6. }

在实际运行过程中,报错:

  1. org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present

问题原因

因为upfile的属性是upfile,而不是file,传递到fileStorageService.upload方法中的时候,找不到file标记的文件内容

解决方案

方案一:让两个方法的参数名一致

方案二:处理upfile对象,让它生成一个新的MutipartFile对象

  1. MultipartFile multipartFile = new MultipartFileDto(PlatformStorage.MinioMutipartFileFieldName.FIELD_NAME, bytes);
  1. //自定义一个MultipartFile
  2. public class MultipartFileDto implements MultipartFile {
  3. private final String name;
  4. private String originalFilename;
  5. private String contentType;
  6. private final byte[] content;
  7. /**
  8. * Create a new MultipartFileDto with the given content.
  9. * @param name the name of the file
  10. * @param content the content of the file
  11. */
  12. public MultipartFileDto(String name, byte[] content) {
  13. this(name, "", null, content);
  14. }
  15. /**
  16. * Create a new MultipartFileDto with the given content.
  17. * @param name the name of the file
  18. * @param contentStream the content of the file as stream
  19. * @throws IOException if reading from the stream failed
  20. */
  21. public MultipartFileDto(String name, InputStream contentStream) throws IOException {
  22. this(name, "", null, FileCopyUtils.copyToByteArray(contentStream));
  23. }
  24. /**
  25. * Create a new MultipartFileDto with the given content.
  26. * @param name the name of the file
  27. * @param originalFilename the original filename (as on the client's machine)
  28. * @param contentType the content type (if known)
  29. * @param content the content of the file
  30. */
  31. public MultipartFileDto(String name, String originalFilename, String contentType, byte[] content) {
  32. this.name = name;
  33. this.originalFilename = (originalFilename != null ? originalFilename : "");
  34. this.contentType = contentType;
  35. this.content = (content != null ? content : new byte[0]);
  36. }
  37. /**
  38. * Create a new MultipartFileDto with the given content.
  39. * @param name the name of the file
  40. * @param originalFilename the original filename (as on the client's machine)
  41. * @param contentType the content type (if known)
  42. * @param contentStream the content of the file as stream
  43. * @throws IOException if reading from the stream failed
  44. */
  45. public MultipartFileDto(String name, String originalFilename, String contentType, InputStream contentStream)
  46. throws IOException {
  47. this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
  48. }
  49. @Override
  50. public String getName() {
  51. return this.name;
  52. }
  53. @Override
  54. public String getOriginalFilename() {
  55. return this.originalFilename;
  56. }
  57. @Override
  58. public String getContentType() {
  59. return this.contentType;
  60. }
  61. @Override
  62. public boolean isEmpty() {
  63. return (this.content.length == 0);
  64. }
  65. @Override
  66. public long getSize() {
  67. return this.content.length;
  68. }
  69. @Override
  70. public byte[] getBytes() throws IOException {
  71. return this.content;
  72. }
  73. @Override
  74. public InputStream getInputStream() throws IOException {
  75. return new ByteArrayInputStream(this.content);
  76. }
  77. @Override
  78. public void transferTo(File dest) throws IOException, IllegalStateException {
  79. FileCopyUtils.copy(this.content, dest);
  80. }
  81. }

方案三:升级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. Could not match supplied host pattern, ignoring: 192.168.0.101

    [root@ansible ansible]# ansible 192.168.0.101 -m ping[WARNING]: Could not match supplied host patter ...

  2. 在前端js worker里使用dom并且加载jquery

    四个工具: nodejs + npm npm安装的jsdom npm安装的jquery npm安装的browserify 网址: browserify: https://browserify.org/ ...

  3. python 给视频加入音频

    1.先去查查  ffmpeg 这个东西   贼强 # 附上大佬博客   https://blog.csdn.net/qq_39752726/article/details/ 104263381?utm ...

  4. Neural Network模型复杂度之Dropout - Python实现

    背景介绍 Neural Network之模型复杂度主要取决于优化参数个数与参数变化范围. 优化参数个数可手动调节, 参数变化范围可通过正则化技术加以限制. 本文从优化参数个数出发, 以dropout技 ...

  5. Linux磁盘占满处理

    按一下操作查看大文件在哪里, 清理大文件. 你切换到 / du -sh * 进入占用多的目录,再使用du -sh *找到下一个大目录. 以此类推,删除无用大文件

  6. springcloud(五) - 网关gateway

    功能介绍 springcloud gateway提供一种以路由的方式,基于Filter链的方式提供网关的基本功能.如安全.监控.限流. 网关:将不同协议的网络段连接到一起的设备,外网进入内网的入口,对 ...

  7. php json_encode 斜杠 反斜杠 转义处理

    $data = str_replace("\\\\n", "\\n", \jsonEncode($data)); // \\n转为\n $data = str_ ...

  8. Vue之使用umy-ui库的u-table解决 el-table当存在大量数据时,界面操作卡顿。

    提示:一.下面的1. 对应 二.下面的1.:2.则对应2. 错误排查:在使用中如果出现:readding 'style' undefined类似错误的, 可以先排查 u-table中height的值引 ...

  9. Win11右键默认显示更多选项的设置

    怎么让Win11右键默认显示更多选项?有很多朋友不喜欢win11系统的右键菜单显示,经常需要多点一次"显示更多选项"才能看到想要的内容,大家想知道如何让win11右键菜单默认显示更 ...

  10. fs.access(path[, mode], callback)