如何使用Spring 5的webClient上传文件

huangapple go评论73阅读模式
英文:

How to upload file with Spring 5 webClient

问题

MultipartBodyBuilder builder = new MultipartBodyBuilder();

builder.part("subject", parametres.getSubject());

builder.part("lifetime", parametres.getLifeTime());

builder.part("comment", parametres.getComment());

builder.part("encrypted", parametres.getEncrypted());

builder.part("file", new FileSystemResource(file)).filename(file.getName()); //file is a java.io.File
ClientResponse response = getClient().post()
     .uri("/messages")
     .body(BodyInserters.fromMultipartData(builder.build()))
     .header(PlinePlexHeaderEnum.HTTP_HEADER_PROCURATION_USER_ID.value(), idUser))
     .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
     .exchange().block();
英文:

I try to upload files with webClient Spring5 (multipart/form-data), but I got 400 Badrequest there is my code (when i try with restTemplate all work fine), don't understand why not work with webclient, any help is precious, thanks in advance...

MultipartBodyBuilder builder = new MultipartBodyBuilder();

builder.part("subject", parametres.getSubject());

builder.part("lifetime", parametres.getLifeTime());

builder.part("comment", parametres.getComment());

builder.part("encrypted", parametres.getEncrypted());

builder.part("file", new FileSystemResource(file)).filename(file.getName()) //file is a java.io.File`
ClientResponse response = getClient().post()
     .uri("/messages")
     .body(BodyInserters.fromMultipartData(builder.build()))
     .header(PlinePlexHeaderEnum.HTTP_HEADER_PROCURATION_USER_ID.value(), idUser))
     .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
     .exchange().block();

答案1

得分: 2

  final WebClient webClient = webClientBuilder.build();
  webClient.post()
      .uri("http://hostname:port/messages")
      .contentType(MediaType.MULTIPART_FORM_DATA)
      .header(PlinePlexHeaderEnum.HTTP_HEADER_PROCURATION_USER_ID.value(), idUser))
      .body(BodyInserters.fromMultipartData(buildMultipartBody(file, parametres)))
      .retrieve()
      .bodyToMono(String.class)
      .block();
}

private MultiValueMap<String, HttpEntity<?>> buildMultipartBody(File file, Parametres parametres) {
  MultipartBodyBuilder builder = new MultipartBodyBuilder();
  builder.part("file", new FileSystemResource(file));
  builder.part("subject", parametres.getSubject());
  builder.part("lifetime", parametres.getLifeTime());
  builder.part("comment", parametres.getComment());
  builder.part("encrypted", parametres.getEncrypted());
  return builder.build();
}
英文:

Try this:

  final WebClient webClient = webClientBuilder.build();
        webClient.post()
            .uri(&quot;http://hostname:port/messages&quot;)
            .contentType(MediaType.MULTIPART_FORM_DATA)
            .header(PlinePlexHeaderEnum.HTTP_HEADER_PROCURATION_USER_ID.value(), idUser))
            .body(BodyInserters.fromMultipartData(buildMultipartBody(file, parametres)))
            .retrieve()
            .bodyToMono(String.class)
            .block();
        }

        private MultiValueMap &lt; String, HttpEntity &lt;&lt; ? &gt;&gt; buildMultipartBody(private File file, private Parametres parametres) {
            MultipartBodyBuilder builder = new MultipartBodyBuilder();
            builder.part(&quot;file&quot;, new FileSystemResource(file));
            builder.part(&quot;subject&quot;, parametres.getSubject());       `
            builder.part(&quot;lifetime&quot;, parametres.getLifeTime());`
            builder.part(&quot;comment&quot;, parametres.getComment());   `
            builder.part(&quot;encrypted&quot;, parametres.getEncrypted());`
            return builder.build();
        }

huangapple
  • 本文由 发表于 2020年10月4日 19:18:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64193940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定