<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免费观看| 亚洲国产欧美国产综合一区| 亚洲av成人无码网站…| 无码av免费一区二区三区试看| 午夜两性色视频免费网站| 亚洲产国偷V产偷V自拍色戒| 亚洲AV成人一区二区三区在线看| 在线免费观看伊人三级电影| 精品免费久久久久久成人影院| 亚洲va久久久久| 国产高清免费在线| 理论秋霞在线看免费| 欧美三级在线电影免费| 久久久无码精品亚洲日韩蜜桃 | 久久青草91免费观看| 在线观看免费精品国产| 亚洲精品在线不卡| 久久久精品午夜免费不卡| 亚洲福利视频一区二区| 亚洲国产精华液2020| 国产禁女女网站免费看| 成人午夜影视全部免费看| 国产成人高清精品免费软件| 日韩在线观看视频免费| 国产AV无码专区亚洲AV毛网站| 99久久免费中文字幕精品| 亚洲国产成人片在线观看无码 | 国产免费久久久久久无码| 日韩免费观看的一级毛片| 有色视频在线观看免费高清在线直播| 久久精品亚洲男人的天堂| 老外毛片免费视频播放| 亚洲精品无码鲁网中文电影| 一级毛片a免费播放王色电影| 亚洲码国产精品高潮在线| 四虎国产精品免费永久在线| 亚洲精品在线播放| 亚洲一区二区三区无码影院| jizz在线免费观看| 中文字幕无码精品亚洲资源网|