paulwong

          Downloading Large Files using Spring WebClient

          https://www.amitph.com/spring-webclient-large-file-download/

          https://github.com/amitrp/spring-examples/blob/main/spring-webflux-webclient/src/main/java/com/amitph/spring/webclients/service/FileDownloaderWebClientService.java

          import lombok.RequiredArgsConstructor;
          import org.springframework.core.io.buffer.DataBuffer;
          import org.springframework.core.io.buffer.DataBufferUtils;
          import org.springframework.stereotype.Service;
          import org.springframework.web.reactive.function.client.WebClient;
          import reactor.core.publisher.Flux;
          import reactor.core.publisher.Mono;

          import java.io.IOException;
          import java.nio.file.Files;
          import java.nio.file.Path;
          import java.nio.file.StandardOpenOption;
          import java.util.Objects;

          @Service
          @RequiredArgsConstructor
          public class FileDownloaderWebClientService {
              private final WebClient webClient;

              /**
               * Reads the complete file in-memory. Thus, only useful for very large file
               
          */
              public void downloadUsingByteArray(Path destination) throws IOException {
                  Mono<byte[]> monoContents = webClient
                          .get()
                          .uri("/largefiles/1")
                          .retrieve()
                          .bodyToMono(byte[].class);

                  Files.write(destination, Objects.requireNonNull(monoContents.share().block()),
                          StandardOpenOption.CREATE);
              }

              /**
               * Reading file using Mono will try to fit the entire file into the DataBuffer.
               * Results in exception when the file is larger than the DataBuffer capacity.
               
          */
              public void downloadUsingMono(Path destination) {
                  Mono<DataBuffer> dataBuffer = webClient
                          .get()
                          .uri("/largefiles/1")
                          .retrieve()
                          .bodyToMono(DataBuffer.class);

                  DataBufferUtils.write(dataBuffer, destination,
                          StandardOpenOption.CREATE)
                          .share().block();
              }

              /**
               * Having using Flux we can download files of any size safely.
               * Optionally, we can configure DataBuffer capacity for better memory utilization.
               
          */
              public void downloadUsingFlux(Path destination) {
                  Flux<DataBuffer> dataBuffer = webClient
                          .get()
                          .uri("/largefiles/1")
                          .retrieve()
                          .bodyToFlux(DataBuffer.class);

                  DataBufferUtils.write(dataBuffer, destination,
                          StandardOpenOption.CREATE)
                          .share().block();
              }
          }

          posted on 2022-09-22 13:14 paulwong 閱讀(267) 評論(0)  編輯  收藏 所屬分類: SPRINGSPRING BOOT

          主站蜘蛛池模板: 新郑市| 南宫市| 兴化市| 绍兴县| 和平县| 通榆县| 镇江市| 湘潭县| 盖州市| 茶陵县| 惠水县| 怀远县| 呼玛县| 固始县| 阳谷县| 阿瓦提县| 昆山市| 安达市| 达拉特旗| 宣化县| 彭山县| 铜山县| 花莲县| 通许县| 广饶县| 新疆| 吴忠市| 永福县| 牡丹江市| 贡嘎县| 萝北县| 丘北县| 石景山区| 南木林县| 双城市| 扎兰屯市| 株洲市| 天全县| 江北区| 建水县| 凤冈县|