Spring Web Reactive客户端

Ozz*_*zie 6 java spring spring-mvc spring-boot spring-webflux

我正在尝试使用Spring Reactive WebClient将文件上传到Spring控制器。控制器非常简单,看起来像这样:

@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> uploadFile(
        @RequestParam("multipartFile") MultipartFile multipartFile,
        @RequestParam Map<String, Object> entityRequest
        ) {
    entityRequest.entrySet().forEach(System.out::println);
    System.out.println(multipartFile);
    return ResponseEntity.ok("OK");
}
Run Code Online (Sandbox Code Playgroud)

当我将此控制器与cURL一起使用时,一切正常

curl -X POST http://localhost:8080/upload -H 'content-type: multipart/form-data;' -F fileName=test.txt -F randomKey=randomValue -F multipartFile=@document.pdf
Run Code Online (Sandbox Code Playgroud)

multipartFile转到正确的参数,其他参数转到Map。

当我尝试从WebClient执行相同操作时,我陷入了困境。我的代码如下所示:

    WebClient client = WebClient.builder().baseUrl("http://localhost:8080").build();

    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    map.set("multipartFile", new ByteArrayResource(Files.readAllBytes(Paths.get("/path/to/my/document.pdf"))));
    map.set("fileName", "test.txt");
    map.set("randomKey", "randomValue");
    String result = client.post()
            .uri("/upload")
            .contentType(MediaType.MULTIPART_FORM_DATA)
            .syncBody(map)
            .exchange()
            .flatMap(response -> response.bodyToMono(String.class))
            .flux()
            .blockFirst();
    System.out.println("RESULT: " + result);
Run Code Online (Sandbox Code Playgroud)

这将导致400错误

{
  "timestamp":1510228507230,
  "status":400,
  "error":"Bad Request",
  "message":"Required request part 'multipartFile' is not present",
  "path":"/upload"
}
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决这个问题?

Ozz*_*zie 7

所以我自己找到了解决方案。事实证明,Spring 确实需要 Content-Disposition 标头来包含上传的文件名,以便将其序列化为控制器中的 MultipartFile。

为此,我必须创建一个支持设置文件名的 ByteArrayResource 子类

public class MultiPartResource extends ByteArrayResource {

  private String filename;

  public MultiPartResource(byte[] byteArray) {
    super(byteArray);
  }

  public MultiPartResource(byte[] byteArray, String filename) {
    super(byteArray);
    this.filename = filename;
  }

  @Nullable
  @Override
  public String getFilename() {
    return filename;
  }

  public void setFilename(String filename) {
    this.filename = filename;
  }
}
Run Code Online (Sandbox Code Playgroud)

然后可以使用此代码在客户端中使用

WebClient client = WebClient.builder().baseUrl("http://localhost:8080").build();

MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();

map.set("fileName", "test.txt");
map.set("randomKey", "randomValue");
ByteArrayResource resource = new MultiPartResource(Files.readAllBytes(Paths.get("/path/to/my/document.pdf")), "document.pdf");

String result = client.post()
        .uri("/upload")
        .contentType(MediaType.MULTIPART_FORM_DATA)
        .body(BodyInserters.fromMultipartData(map))
        .exchange()
        .flatMap(response -> response.bodyToMono(String.class))
        .flux()
        .blockFirst();
System.out.println("RESULT: " + result);
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记调用`map.set("multipartFile", resource)` (2认同)