多部分文件中不支持的媒体类型 api
Unsupported Media Type from multi part file api
POSTMAN 总是给出 415 不支持的媒体类型错误。 Header 包含 multipart/form-data 和下面的 CURL 调用中的边界。还尝试用 RequestBody 替换 RequestPart 但没有成功。
当使用 FilePart 时,我们是否需要以任何不同的方式从 spring 5 调用多部分文件上传 api?
REST控制器:
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public void uploads(@RequestPart("filePart") Flux<Part> fileparts) {
....
}
卷曲:
curl -X POST \
http://localhost:8080/upload \
-H 'accept: application/json' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'postman-token: 2e850843-13d0-32d3-8734-227242da3303' \
-F filePart=@abc.txt
输出:
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain' not supported",
编辑
将上传参数从 @RequestPart("filePart") Flux<Part> fileparts
更改为 @RequestParam("file") MultipartFile file
但是有效。
我们不能对 RequestPart 使用相同的 curl 调用吗?
问题是由于 pom.xml 同时添加了 spring-webmvc 和 spring-webflux 作为依赖。 spring-webmvc
和spring-webflux
好像不能同时启用。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>${spring.boot.version}</version>
</dependency>
POSTMAN 总是给出 415 不支持的媒体类型错误。 Header 包含 multipart/form-data 和下面的 CURL 调用中的边界。还尝试用 RequestBody 替换 RequestPart 但没有成功。
当使用 FilePart 时,我们是否需要以任何不同的方式从 spring 5 调用多部分文件上传 api?
REST控制器:
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public void uploads(@RequestPart("filePart") Flux<Part> fileparts) {
....
}
卷曲:
curl -X POST \
http://localhost:8080/upload \
-H 'accept: application/json' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'postman-token: 2e850843-13d0-32d3-8734-227242da3303' \
-F filePart=@abc.txt
输出:
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain' not supported",
编辑
将上传参数从 @RequestPart("filePart") Flux<Part> fileparts
更改为 @RequestParam("file") MultipartFile file
但是有效。
我们不能对 RequestPart 使用相同的 curl 调用吗?
问题是由于 pom.xml 同时添加了 spring-webmvc 和 spring-webflux 作为依赖。 spring-webmvc
和spring-webflux
好像不能同时启用。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>${spring.boot.version}</version>
</dependency>