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 閱讀(268) 評論(0)  編輯  收藏 所屬分類: SPRINGSPRING BOOT

          主站蜘蛛池模板: 壤塘县| 澳门| 海原县| 都昌县| 万荣县| 承德县| 阿拉尔市| 黄冈市| 冕宁县| 玉田县| 万荣县| 屏南县| 峡江县| 扶沟县| 泸西县| 南部县| 邵阳县| 东平县| 会同县| 德庆县| 婺源县| 射阳县| 南投市| 临沧市| 霍邱县| 育儿| 岗巴县| 炎陵县| 裕民县| 福泉市| 东台市| 丹巴县| 互助| 兴国县| 册亨县| 张家川| 嵊泗县| 武穴市| 荆门市| 大姚县| 昆明市|