【发布时间】: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