我正在使用 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"
}
我从 random 中找到的我必须使用的 stackoverflow 问题
@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 配置 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
最佳答案
如 documentation说:
The DefaultServerWebExchange uses the configured
HttpMessageReader<MultiValueMap<String, Part>>
to parse multipart/form-data content into a MultiValueMap.To parse multipart data in streaming fashion, you can use the Flux returned from an HttpMessageReader instead.
用几句话,你需要做这样的事情:
@RequestMapping(path = "/uploadFile", method = RequestMethod.POST,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Flux<String> uploadFile(@RequestBody Flux<Part> parts) {
//...
}
看这个example
关于Spring WebFlux - bodyType=org.springframework.web.multipart.MultipartFile 不支持内容类型 'application/xml',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55632633/