发送已接收的Spring FilePart而不保存

时间:2018-10-04 21:48:17

标签: java spring spring-webflux

我需要使用WebClient将RestController中收到的FilePart发送到API, 我该怎么办?

找到一个将图像保存到磁盘的示例。

    private static String UPLOAD_ROOT = "C:\\pics\\";

public Mono<Void> checkInTest(@RequestPart("photo") Flux<FilePart> photoParts,
                              @RequestPart("data") CheckInParams params, Principal principal) {
    return saveFileToDisk(photoParts);
}

private Mono<Void> saveFileToDisk(Flux<FilePart> parts) {
    return parts
            .log("createImage-files")
            .flatMap(file -> {
                Mono<Void> copyFile = Mono.just(Paths.get(UPLOAD_ROOT, file.filename()).toFile())
                        .log("createImage-picktarget")
                        .map(destFile -> {
                            try {
                                destFile.createNewFile();
                                return destFile;
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                        })
                        .log("createImage-newfile")
                        .flatMap(file::transferTo)
                        .log("createImage-copy");

                return Mono.when(copyFile)
                        .log("createImage-when");
            })
            .log("createImage-flatMap")
            .then()
            .log("createImage-done");
}

然后再次阅读并发送到注释服务器

.map(destFile -> {
                            MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
                            try {
                                map.set("multipartFile", new ByteArrayResource(FileUtils.readFileToByteArray(destFile)));
                            } catch (IOException ignored) {
                            }
                            map.set("fileName", "test.txt");
                            WebClient client = WebClient.builder().baseUrl("http://localhost:8080").build();

                            return client.post()
                                    .uri("/upload")
                                    .contentType(MediaType.MULTIPART_FORM_DATA)
                                    .syncBody(map)
                                    .exchange(); //todo handle errors???
                        }).then()

是否可以避免保存文件?

1 个答案:

答案 0 :(得分:0)

我会提到@Abhinaba Chakraborty的解决方案
https://stackoverflow.com/a/62745370/4551411

中提供 <块引用>

大概是这样的:

  @PostMapping(value = "/images/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public Mono<ResponseEntity<Void>> uploadImages(@RequestPart("files") Flux<FilePart> fileParts) {
    return fileParts
        .flatMap(filePart -> {
          return webClient.post()
              .uri("/someOtherService")
              .body(BodyInserters.fromPublisher(filePart.content(), DataBuffer.class))
              .exchange()
              .flatMap(clientResponse -> {
                //some logging
                return Mono.empty();
              });
        })
        .collectList()
        .flatMap(response -> Mono.just(ResponseEntity.accepted().build()));
  }
<块引用>

这接受 MULTIPART FORM DATA,您可以在其中附加多个图像文件并将它们上传到其他服务。