[英]Spring WebFlux File Upload: Unsupported Media Type 415 with Multipart upload
I'm running into some issues handling a file upload using spring's reactive framework. 我在使用Spring的反应式框架处理文件上传时遇到一些问题。 I think I'm following the docs, but can't get away from this
415
/ Unsupported Media Type
issue. 我认为我正在关注文档,但是无法摆脱
415
/ Unsupported Media Type
问题。
My controller looks like below (as per the example here: https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-multipart-forms ) 我的控制器如下所示(按照此处的示例: https : //docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-multipart-forms )
package com.test.controllers;
import reactor.core.publisher.Flux;
import org.springframework.http.MediaType;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.codec.multipart.Part;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Flux<String> uploadHandler(@RequestBody Flux<Part> parts) {
return parts
.filter(part -> part instanceof FilePart)
.ofType(FilePart.class)
.log()
.flatMap(p -> Flux.just(p.filename()));
}
}
POSTing to this endpoint though, always gives me the same output: 但是,发布到此端点始终会给我相同的输出:
curl -X POST -F "data=@basic.ppt" http://localhost:8080/upload
---
"Unsupported Media Type","message":"Content type 'multipart/form-data;boundary=------------------------537139718d79303c;charset=UTF-8' not supported"
I've attempted to use @RequestPart("data")
too, but get a similar Unsupported Media Type
error, albeit with the content type of the file. 我也尝试过使用
@RequestPart("data")
,但是出现类似的Unsupported Media Type
错误,尽管文件的内容类型也是如此。
It seems that Spring is having issues converting these to a Part
..? 看来Spring在将这些转换为
Part
..?时遇到问题。 I'm stuck - any help is apprecitated! 我被困住了-我们不胜感激!
Well, it's not a direct answer for your question, because I use functional endpoints, but I hope it will help you somehow. 嗯,这不是您的问题的直接答案,因为我使用功能性端点,但希望它能以某种方式对您有所帮助。
import org.springframework.context.annotation.Bean;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.codec.multipart.Part;
import org.springframework.stereotype.Controller;
import org.springframework.web.reactive.function.BodyExtractors;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import java.io.File;
import java.util.Map;
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
@Controller
public class FileUploadController {
@Bean
RouterFunction<ServerResponse> apiRoutes() {
return nest(path("/api"),
route(POST("/upload"), fileUpload()));
}
private HandlerFunction<ServerResponse> fileUpload() {
return request -> {
return request.body(BodyExtractors.toMultipartData()).flatMap(parts -> {
Map<String, Part> map = parts.toSingleValueMap();
final FilePart filePart = (FilePart) map.get("file");
final String dir = "C:\\JDeveloper\\mywork\\Spring\\SpringTest\\webflux-file-upload\\uploaded";
filePart.transferTo(new File(dir + "/" + filePart.filename()));
return ServerResponse.ok().body(fromObject("ok, file uploaded"));
}
);
};
}
}
You can upload a file with curl like this: 您可以上传带有curl的文件,如下所示:
curl -F "file=@C:\Users\Wojtek\Desktop\img-5081775796112008742.jpg" localhost:8080/api/fileupload
Thanks @kojot for your answer, but in this case I discovered the issue was my inclusion of spring-webmvc
transiently in addition to spring-webflux
. 感谢@kojot的回答,但是在这种情况下,我发现问题是除了
spring-webflux
之外,我spring-webmvc
暂时包含了spring-webflux
。 Your solution would likely have worked too, but I wanted to stick with the Controller style so ended up forcibly excluding spring-webmvc
from my build.gradle
: 您的解决方案可能也可以使用,但是我想坚持使用Controller样式,因此最终从
build.gradle
强制排除了spring-webmvc
build.gradle
:
configurations {
implementation {
exclude group: 'org.springframework', module: 'spring-webmvc'
}
}
After that it worked as documented. 在那之后,它按照记录工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.