我正在使用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"
}我从一个随机性堆栈溢出问题中发现,我必须使用@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"
}下面是邮递员配置n,它非常直截了当,我只是用一个post请求来调用,并且只修改了如图中所示的主体。

顺便说一下,我还在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发布于 2019-04-11 13:32:40
如文档所述:
DefaultServerWebExchange使用配置的
HttpMessageReader<MultiValueMap<String, Part>>将多部分/表单数据内容解析为MultiValueMap。 要以流的方式解析多部分数据,可以使用从HttpMessageReader返回的Flux。
少说几句话,你就需要做这样的事情:
@RequestMapping(path = "/uploadFile", method = RequestMethod.POST,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Flux<String> uploadFile(@RequestBody Flux<Part> parts) {
//...
}看看这个示例
https://stackoverflow.com/questions/55632633
复制相似问题