[英]How to stream file from Multipart/form-data in Spring WebFlux
I want to receive Multipart/form-data from a client (frontend for example).我想从客户端(例如前端)接收 Multipart/form-data。 And then stream file content of form-data to another backend service.然后将表单数据的 stream 文件内容发送到另一个后端服务。
For now i can read the whole file and pass it somewhere via byte[] (base64 string) like this:现在我可以读取整个文件并通过 byte[] (base64 字符串)将其传递给某处,如下所示:
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Mono<ResponseType> upload(@RequestPart("document") FilePart document,
@RequestPart("stringParam") String stringParam) {
return service.upload(document, stringParam);
}
// Casually convert to single byte array...
private Mono<byte[]> convertFilePartToByteArray(FilePart filePart) {
return Mono.from(filePart
.content()
.map(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
DataBufferUtils.release(dataBuffer);
return bytes;
}));
}
There're a few problems with this approach:这种方法存在一些问题:
I've already tried sending file via asyncPart of WebClientBuilder:我已经尝试通过 WebClientBuilder 的 asyncPart 发送文件:
MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.asyncPart("document", document.content(), DataBuffer.class);
But i'm getting an error:但我收到一个错误:
java.lang.IllegalStateException: argument type mismatch
Method [public reactor.core.publisher.Mono<> upload(**org.springframework.http.codec.multipart.FilePart**,java.lang.String)] with argument values:
[0] [type=**org.springframework.http.codec.multipart.DefaultParts$DefaultFormFieldPart**]
UPD : full code, which generates error UPD :完整代码,会产生错误
// External controller for client.
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, value = "/v2")
public Mono<DocumentUploadResponse> uploadV2(@RequestPart("document") FilePart document,
@RequestPart("stringParam") String stringParam) {
MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.asyncPart("document", document.content(), DataBuffer.class);
builder.part("stringParam", stringParam);
WebClient webClient = webClientBuilder.build();
return webClient.post()
.uri("URL_TO_ANOTHER_SERVICE")
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(builder.build()))
.retrieve()
.bodyToMono(FileMetaDto.class)
.map(DocumentUploadResponse::new);
}
// Internal service controller.
@PostMapping(path = "/upload/v2", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Mono<FileMetaDto> upload(@RequestPart("document") FilePart document,
@RequestPart("stringParam") String stringParam) {
return ...;
}
Looks like i was managed to stream file, working code below:看起来我被管理到 stream 文件,工作代码如下:
// External controller for client.
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, value = "/v2")
public Mono<DocumentUploadResponse> uploadV2(@RequestPart("document") FilePart document,
@RequestPart("stringParam") String stringParam) {
MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.asyncPart("document", document.content(), DataBuffer.class).filename(document.filename());
builder.part("stringParam", stringParam);
WebClient webClient = webClientBuilder.build();
return webClient.post()
.uri("URL_TO_ANOTHER_SERVICE")
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(builder.build()))
.retrieve()
.bodyToMono(FileMetaDto.class)
.map(DocumentUploadResponse::new);
}
// Internal service controller.
@PostMapping(path = "/upload/v2", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Mono<FileMetaDto> upload(@RequestPart("document") FilePart document,
@RequestPart("stringParam") String stringParam) {
return ...;
}
In the original question code i've been missing:在我丢失的原始问题代码中:
builder.asyncPart("document", document.content(), DataBuffer.class) .filename(document.filename()); builder.asyncPart("document", document.content(), DataBuffer.class) .filename(document.filename());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.