英文:
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("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(private File file, private 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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论