【问题标题】:Spring Boot 2.0.2 MultipartConfigElement not being configured for MultipartFile没有为 MultipartFile 配置 Spring Boot 2.0.2 MultipartConfigElement
【发布时间】:2018-11-21 06:55:16
【问题描述】:

我正在使用 Spring Boot 2.0.2.RELEASE,但无法为 REST 控制器端点上传文件。

在this 开始之后,它说:

作为自动配置 Spring MVC 的一部分,Spring Boot 将创建一个 MultipartConfigElement bean 并为文件上传做好准备。

所以,理论上,它应该可以在没有任何额外配置的情况下工作,但看起来这个MultipartConfigElement 根本没有被配置。

我收到了这个警告:

WARN .a.w.r.e.DefaultErrorWebExceptionHandler: Failed to handle request [POST http://localhost:8080/upload]: Response status 400 with reason "Required MultipartFile parameter 'file' is not present"

我的 Spring 应用启动器很简单:

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

我的终点是:

@RestController
public class MyController {

@PostMapping("/upload")
public String hash(@RequestParam("file") MultipartFile file) {
    final String test = file.getContentType();
}

这是我与邮递员一起发送的方式:

我还确保取消标记邮递员设置的任何默认内容类型,但没有成功。 我可能做错了什么?

【问题讨论】:

  • 您是否在 Postman 中指定了任何标题?
  • 指定与否,它不起作用。 :(
  • 尝试设置标题“Content-Type”:“application/x-www-form-urlencoded”。
  • 已经试过了。
  • 那是因为 webflux 不是建立在 servlet 上,而是建立在 netty 上,因此你没有得到 servlet 3+ 的多部分支持。

标签: java spring-boot multipart


【解决方案1】:

首先,将其添加到您的属性文件中

servlet.multipart.enabled=true servlet.multipart.max-file-size=20M

并将 CommonsMultipartResolver bean 创建为 (name = "multipartResolver")

【讨论】:

  • 在 Spring 文档中哪里可以找到这个?
  • servlet.multipart.enabled 在 Spring boot 中默认设置为 true
【解决方案2】:

同样的问题,但是我通过这种方式得到了这些文件。

您可以在这个 github 存储库中找到这些文件:

gs-uploading-files

您只需下载此应用程序的 zip 文件,然后找到您需要的文件。

【讨论】:

    猜你喜欢
    • 2016-12-24
    • 1970-01-01
    • 2017-11-27
    • 2021-05-29
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    相关资源
    最近更新 更多