I'm attempting to "pass through" an upload from a controller to another server as part of a multipart request using WebClient. I've finally got the file upload working, but am having trouble getting the file name in a streaming fashion from the FilePart. I'm not sure this is even possible?

This is the piece of code that is causing the issue:

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();

Note that I've had to extract the Flux<DataBuffer> from the Flux<FilePart> as Spring was unable to find a writer for FilePart (No suitable writer found for part: file).

Is it possible to set the content-disposition header in this fashion?

0 Answers