【问题标题】:Postman - Required MultipartFile parameter is not present - Spring, Java [duplicate]Postman - 所需的 MultipartFile 参数不存在 - Spring,Java [重复]
【发布时间】:2017-01-07 16:58:59
【问题描述】:

编辑

这个问题不同于:jQuery Ajax file upload : Required MultipartFile parameter 'file' is not present 不同之处在于他们使用 jQuery 和 Ajax,而我使用的是 REST 客户端 - 'Postman'

因此,我不得不将其完全删除,而不是将 Content-Type 设置为 false。

另外,在搜索“Postman”的答案时,我相信人们会跳过其中包含 jQuery 和 Ajax 的问题,这就是发生在我身上的事情

结束编辑

我在 Java8 上使用 Spring MVC Web 应用程序并在 tomcat7.x 上运行它。 Spring 版本为:4.2.6.RELEASE,javax servlet 版本为:3.0.1

context.xml

...

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- max upload size in bytes -->
    <property name="maxUploadSize" value="5242880" /> <!-- 5MB -->

    <!-- max size of file in memory (in bytes) -->
    <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->

</bean>

...

controller.java

...
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseStatus(value = HttpStatus.CREATED)
public void importTranslations(@RequestParam (name = "myfile") MultipartFile myfile) {
    myService.doSomething(myfile);
}
...

问题来了

我使用 Postman 发送 *.zip 文件。路径是正确的,一切看起来都很好,但是 spring 抛出了一个异常: "所需的 MultipartFile 参数 'myfile' 不存在"

这是 Postman 的截图: 所以文件在那里并且键名是正确的。 一切似乎都很好,但是,我得到了错误

【问题讨论】:

  • @Jens - 请参阅我在问题开头的编辑,关于可能的重复。我花了一段时间才找到答案,这是因为我搜索了 Postman 并忽略了有关 jQuery 和 Ajax 的答案

标签: java spring tomcat multipartform-data postman


【解决方案1】:

在stackoverflow中搜索了很多之后,我发现了这个问题:jQuery Ajax file upload : Required MultipartFile parameter 'file' is not present

我尝试将 Postman 中的 Content-Type 标头设置为 false,但出现错误。 当我删除 Content-Type 标头时,它起作用了!!

希望这对某人有所帮助

【讨论】:

  • 节省了我的一天.. 太棒了。
  • @anil-prajapati - 很高兴能够提供帮助!
猜你喜欢
  • 2021-04-12
  • 2016-07-13
  • 1970-01-01
  • 2014-05-08
  • 1970-01-01
  • 2017-02-12
  • 1970-01-01
  • 2014-09-10
  • 2015-01-21
相关资源
最近更新 更多