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

    主站蜘蛛池模板: 免费看黄福利app导航看一下黄色录像| 亚洲Aⅴ无码专区在线观看q| 热99re久久精品精品免费| 日本免费xxxx色视频| 91精品免费不卡在线观看| 久久精品一区二区免费看| 野花香高清在线观看视频播放免费 | 精品国产麻豆免费人成网站| 国产一级在线免费观看| 成人精品视频99在线观看免费| 曰韩无码AV片免费播放不卡| 久久久久久亚洲av无码蜜芽| 亚洲成a∧人片在线观看无码| 亚洲色成人WWW永久在线观看| 亚洲高清一区二区三区| 中文字幕无码亚洲欧洲日韩| 亚洲熟妇无码AV不卡在线播放| 亚洲中文字幕一区精品自拍| 亚洲爆乳少妇无码激情| 黄色a级片免费看| 久久国产精品免费一区二区三区| eeuss影院免费直达入口| 中文在线免费视频| 久久久久久成人毛片免费看| 91久久青青草原线免费| 韩国免费一级成人毛片| 麻豆国产人免费人成免费视频| 亚洲?v无码国产在丝袜线观看| 久久亚洲色一区二区三区| 久久综合图区亚洲综合图区| 亚洲制服丝袜在线播放| 亚洲AV无码一区二区三区久久精品| 深夜a级毛片免费无码| 91国内免费在线视频| 永久看日本大片免费35分钟| 成人毛片免费在线观看| 亚洲裸男gv网站| 精品亚洲麻豆1区2区3区| 亚洲成a∨人片在无码2023| 国产久爱免费精品视频| 亚洲免费视频播放|