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

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

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

有没有办法做到这一点?

【问题讨论】:

    标签: spring-boot file-upload streaming spring-webflux


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

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 2022-01-21
      • 2018-01-13
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 2021-11-12
      相关资源
      最近更新 更多