可以在Multipart请求中从Flux <filepart>设置Content-Disposition标头吗?

时间:2019-05-23 12:45:38

标签: java spring spring-webflux

我尝试使用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标头?

0 个答案:

没有答案