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

          主站蜘蛛池模板: 宜章县| 麻阳| 三原县| 长丰县| 濮阳县| 阳原县| 阿拉尔市| 洪江市| 珲春市| 庆城县| 英德市| 和田市| 石家庄市| 古丈县| 大埔县| 永登县| 西乌珠穆沁旗| 渭南市| 靖西县| 邵东县| 宜都市| 江西省| 肇东市| 连山| 德令哈市| 宁夏| 嘉禾县| 开原市| 革吉县| 鱼台县| 康保县| 海阳市| 荃湾区| 新密市| 宁陕县| 通榆县| 广饶县| 新安县| 荆门市| 高唐县| 高要市|