GOX*_*LUS 5 spring spring-boot spring-web spring-webflux
我正在使用spring-webflux并想要上传文件......一切都很好,spring-web但是当涉及到时webflux我不知道出了什么问题。
小心区别...我正在使用:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
假设我们有以下内容@RestController,因为Spring Web它的作用就像魅力:
@PostMapping(value = "/uploadFile")
public Response uploadFile(@RequestParam("file") MultipartFile file) {
}
Run Code Online (Sandbox Code Playgroud)
现在尝试同样的操作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"
}
Run Code Online (Sandbox Code Playgroud)
我从一个随机的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"
}
Run Code Online (Sandbox Code Playgroud)
即使使用.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"
}
Run Code Online (Sandbox Code Playgroud)
下面是邮递员配置,非常简单,我只是用一个帖子请求进行调用,并仅修改了正文,如图所示。
顺便说一句,我也在 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
Run Code Online (Sandbox Code Playgroud)
正如文档所述:
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) {
//...
}
Run Code Online (Sandbox Code Playgroud)
看这个例子
| 归档时间: |
|
| 查看次数: |
8957 次 |
| 最近记录: |