<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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

    主站蜘蛛池模板: 国产精品免费无遮挡无码永久视频 | 日本免费电影一区| 毛片基地免费观看| 永久免费av无码网站大全| 精品国产成人亚洲午夜福利| 亚洲va久久久久| 日本亚洲免费无线码| 亚洲福利电影在线观看| 99re热精品视频国产免费| 24小时免费直播在线观看| 亚洲日本乱码一区二区在线二产线| 国产亚洲综合视频| 久久免费香蕉视频| 久久国产免费福利永久| 亚洲乱码精品久久久久..| 成年网站免费入口在线观看| 亚洲一级特黄大片无码毛片 | 亚洲免费中文字幕| 黄色片在线免费观看| 亚洲爆乳无码精品AAA片蜜桃| 中文字幕在线免费看| 亚洲国产美国国产综合一区二区 | 永久免费av无码不卡在线观看| 亚洲人配人种jizz| 国产99视频免费精品是看6| h视频免费高清在线观看| 97国产免费全部免费观看| 国产成人亚洲综合网站不卡| 国产高清视频在线免费观看| 亚洲精品白浆高清久久久久久| 久久精品无码精品免费专区| 亚洲av永久无码嘿嘿嘿| 国产在线a不卡免费视频| 一区二区三区免费高清视频| 亚洲最大的成网4438| 午夜电影免费观看| 中国国语毛片免费观看视频| 亚洲福利电影一区二区?| 免费人成激情视频| 午夜免费福利片观看| 亚洲国产精品va在线播放|