我尝试使用WebClient
作为多部分请求的一部分,将从控制器上传的内容“传递”到控制器。我终于可以上载文件了,但是在从FilePart
以流方式获取文件名时遇到了麻烦。我不确定这是否可能?
这是导致问题的代码片段:
private MultiValueMap<String, HttpEntity<?>> getMultipartPayload(Flux<FilePart> fileUploadFlux) {
MultipartBodyBuilder builder = new MultipartBodyBuilder();
Flux<DataBuffer> dataBufferFlux = fileUploadFlux.flatMap(Part::content);
// spring doesn't have an encoder for FilePart, so we must extract the DataBuffer
builder.asyncPart("file", dataBufferFlux, DataBuffer.class).headers(h -> {
// need to set content-disposition header
// but - this doesn't work as the filename is still a flux
h.setContentDispositionFormData("file", fileUploadFlux.map(FilePart::filename));
});
return builder.build();
}
请注意,由于Spring无法为Flux<DataBuffer>
(Flux<FilePart>
)找到作者,我不得不从FilePart
中提取No suitable writer found for part: file
。
是否可以以此方式设置content-disposition标头?