【发布时间】:2019-09-02 02:20:36
【问题描述】:
我正在使用spring-webflux 并想上传文件.... 仅使用spring-web 一切都很好,但是当涉及webflux 时我不知道出了什么问题。
注意区别......我正在使用:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
假设我们有以下 @RestController ,对于 Spring Web 它就像魅力一样:
@PostMapping(value = "/uploadFile")
public Response uploadFile(@RequestParam("file") MultipartFile file) {
}
现在尝试对Spring-webflux 进行相同操作会产生以下错误:
{
"timestamp": "2019-04-11T13:31:01.705+0000",
"path": "/upload",
"status": 400,
"error": "Bad Request",
"message": "Required MultipartFile parameter 'file' is not present"
}
我从randomstackoverflow 问题中发现我必须使用@RequestPart 而不是@RequestParam,但现在我收到以下错误,我不知道为什么会发生这种情况?
错误如下:
{
"timestamp": "2019-04-11T12:27:59.687+0000",
"path": "/uploadFile",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'application/xml' not supported for bodyType=org.springframework.web.multipart.MultipartFile"
}
即使使用.txt 文件也会产生相同的错误:
{
"timestamp": "2019-04-11T12:27:59.687+0000",
"path": "/uploadFile",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'application/xml' not supported for bodyType=org.springframework.web.multipart.MultipartFile"
}
下面是Postman Configuration,它非常简单,我只是用一个帖子请求调用并且只修改了如图所示的正文。
顺便说一句,我也在 application.properties 上添加了所需的属性:)
## MULTIPART (MultipartProperties)
# Enable multipart uploads
spring.servlet.multipart.enabled=true
# Threshold after which files are written to disk.
spring.servlet.multipart.file-size-threshold=2KB
# Max file size.
spring.servlet.multipart.max-file-size=200MB
# Max Request Size
spring.servlet.multipart.max-request-size=215MB
【问题讨论】:
-
好吧,如果您查看答案,您会发现多部分的内容类型不能是 application/xml。就是这样。
-
@JEY 对于 text/plain 或我添加的任何文件,我都会遇到同样的错误,先生 :)
-
我认为您的请求有问题,因为我创建了一个示例项目gist.github.com/Julien-Eyraud/4a9384e11372004d0218a9e8a961108a,它可以正常工作
-
@JEY 我遵循了本教程 :) callicoder.com/… 但他正在使用
spring-web我们正在使用spring-webflux并且所有问题都存在。我对spring-web使用了相同的请求并且它有效:'( 我整天都在为它解决问题:(
标签: spring spring-boot spring-webflux spring-web