#spring #spring-boot #spring-webflux #spring-webclient
Вопрос:
Я загружаю файл с помощью WebClient в соответствии со следующим:
public void uploadFile(String myStr, File file) {
webClient.post().uri("/file/upload/" myStr "/endpoint")
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(fromMultiValueMap(file))).retrieve()
.bodyToMono(String.class).onErrorResume(WebClientResponseException.class, e -> {
if (e.getStatusCode() == HttpStatus.CONFLICT) {
log.info("{}", "ErrorCode:" e.getStatusCode() ":" file.getName() "-The File already exists...");
return Mono.just(e.getResponseBodyAsString());
}
return Mono.error(e);
}).block();
}
И мой fromMultiValueMap
метод выглядит так:
public MultiValueMap<String, HttpEntity<?>> fromMultiValueMap(File file) {
MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.part("file", new FileSystemResource(file));
builder.part("name", file.getName());
builder.part("uploadedBy", "theName");
builder.part("foo", "blah");
builder.part("bar", "blah");
return builder.build();
}
Здесь все хорошо. Вообще никаких проблем.
Теперь вместо File file
у меня есть OutputStream os
и вместо fromMultiValueMap
у меня Map<String, String>
теперь есть объект, как показано ниже:
public void uploadFile(String myStr, OutputStream os, Map<String, String> map) {
webClient.post().uri("/file/upload/" myStr "/endpoint")
.contentType(MediaType.MULTIPART_FORM_DATA)
//.body(BodyInserters.fromMultipartData(fromMultiValueMap(file)))
// My brain is dead here. Not sure what to do. Need help here.
.retrieve()
.bodyToMono(String.class).onErrorResume(WebClientResponseException.class, e -> {
if (e.getStatusCode() == HttpStatus.CONFLICT) {
log.info("{}", "ErrorCode:" e.getStatusCode() ":" ????? "-The File already exists...");
return Mono.just(e.getResponseBodyAsString());
}
return Mono.error(e);
}).block();
}
Как я могу использовать поток вывода и карту для достижения вышеуказанного?
Я попытался найти помощь здесь: https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-client-body-multipart но не смог.