Spring Webflux上传大图像文件并以流式方式使用WebClient发送文件[关闭]

Posted

技术标签:

【中文标题】Spring Webflux上传大图像文件并以流式方式使用WebClient发送文件[关闭]【英文标题】:Spring Webflux Upload Large Image File and Send The File with WebClient in Streaming Way [closed] 【发布时间】:2020-10-14 03:40:51 【问题描述】:

我正在使用 spring webflux 功能样式。 我想创建一个接受大图像文件的端点,并使用 webClient 以流的方式将此文件发送到另一个服务。

所有文件处理都应该以流的方式进行,因为我不想因为内存不足而迷恋我的应用程序。

有没有办法做到这一点?

【问题讨论】:

【参考方案1】:

大概是这样的:

  @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,您可以在其中附加多个图像文件并将它们上传到另一个服务。

【讨论】:

以上是关于Spring Webflux上传大图像文件并以流式方式使用WebClient发送文件[关闭]的主要内容,如果未能解决你的问题,请参考以下文章