This works for me. Just change from Flux to Mono. Seem there is no converter for Flux<byte[]>

@GetMapping(value = "/attachment/{fileId}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public Mono<byte[]> get(@PathVariable String fileId) {

    return this.webClient.get()
            .uri(builder ->
                    builder.scheme("https")
                            .host("external-service.com")
                            .path("/{fileId}")
                            .build(fileId)
            ).attributes(clientRegistrationId("credentials"))
            .accept(MediaType.APPLICATION_OCTET_STREAM)
            .retrieve()
            .bodyToMono(byte[].class);
}
Answer from Chayne P. S. on Stack Overflow
🌐
amitph
amitph.com › home › spring › downloading large files using spring webclient
Downloading Large Files using Spring WebClient - amitph
November 22, 2024 - This tutorial focuses on accessing a large file from an external service using Spring WebClient. We will first study using Mono publisher to download a file in the form of byte[] (byte array).
Discussions

Download and process large data efficiently with WebClient?
FileOutputStream fout = new FileOutputStream(new File("bla.o")) URLConnection c = ... // google how to get a connection. BufferInputStream bis = new BufferInputStream(c.getInputStream()); byte buffer[] = new byte[1024]; while(bis.next()) { bis.read(buffer); fout.write(buffer); } System.out.println("done"); just out of my head. Without reactors. Don't know why you are using it in this use-case. More on reddit.com
🌐 r/SpringBoot
4
3
October 16, 2022
Spring WebClient download and upload file - Stack Overflow
I need to create a process that downloads a file from an API endpoint and uploads it to another API endpoint. The file has a maximum size of 100MB but we will have many processes running in paralle... More on stackoverflow.com
🌐 stackoverflow.com
Unable get fileName from Resource when using WebClient. Content-Disposition header is ignored.
I want to download file from server via WebClient and get body with filename, but Content-Disposition header is ignored. webClient.get() // some code .retrieve() .toEntity(Resource::class.java) ... spring-framework/spring-webflux/src/main/java/org/springframework/web/reactive/function/clie... More on github.com
🌐 github.com
2
November 9, 2020
kotlin - Downloading large file with spring webClient - Stack Overflow
I decided to use spring webClient in order to get large files. Here is my method · suspend fun downloadDocument( documentId: UUID ): InputStream = webClient.get() .uri("") .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_OCTET_STREAM_VALUE) .retrieve() .bodyToFlux() ... More on stackoverflow.com
🌐 stackoverflow.com
August 27, 2021
🌐
Baeldung
baeldung.com › home › spring › spring web › stream large byte[] to file with webclient
Stream Large Byte[] to File With WebClient | Baeldung
January 8, 2024 - client='com.baeldung.streamlargefile.client.LargeFileDownloadWebClient' java -Xmx32m -cp /tmp/app.jar $client $endpoint /tmp/download.dat · In the end, we’ll get a successful output, even though our application had less total memory than the size of our file: ... In this article, we learned different ways to use WebClient to download an arbitrarily large file.
🌐
Reddit
reddit.com › r/springboot › download and process large data efficiently with webclient?
r/SpringBoot on Reddit: Download and process large data efficiently with WebClient?
October 16, 2022 -

Very similar to

Spring WebClient: How to stream large byte[] to file? https://stackoverflow.com/questions/56210186/spring-webclient-how-to-stream-large-byte-to-file/60725206#60725206

I'm fetching CSV data from a remote Rest endpoint, check for patterns within the data (against a list of user e-mail).

Basically I'm asking how to run the incoming data I'm fetching through some filter / CSV parser without ever storing data on disk or keeping the entire data in memory.

(In case of a match the matching CSV row is passed to another service class within my application.)

PS:

I found a semi working(*) solution here https://www.baeldung.com/spring-reactive-read-flux-into-inputstream

but what I dislike about this solution is having to spawn another Thread and the Thread.sleep. Or am I over thinking? (*) the code doesn't really work as it fills up the buffer until... DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144

🌐
Stack Overflow
stackoverflow.com › questions › 68498718 › spring-webclient-download-and-upload-file
Spring WebClient download and upload file - Stack Overflow
The current code is storing the file in memory because the tests with big files are throwing OutOfMemoryError. ... public Mono<SyncStatusType> copy(Strin path1, String path2) { return this.download(path1) .collectList() .flatMap(dataBuffers -> this.upload(path2, Mono.just(dataBuffers).flatMapMany(Flux::fromIterable))) .map(item -> SyncStatusType.SUCCESSFUL); } ... private Flux<DataBuffer> download(String path) { return this.getWebClient() .get() .uri(uriBuilder -> uriBuilder .path(path) .build()) .retrieve() .bodyToFlux(DataBuffer.class); }
🌐
GitHub
github.com › amitrp › spring-examples › blob › main › spring-webflux-webclient › src › main › java › com › amitph › spring › webclients › service › FileDownloaderWebClientService.java
spring-examples/spring-webflux-webclient/src/main/java/com/amitph/spring/webclients/service/FileDownloaderWebClientService.java at main · amitrp/spring-examples
import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; · @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); ·
Author   amitrp
🌐
GitHub
github.com › spring-projects › spring-framework › issues › 26053
Unable get fileName from Resource when using WebClient. Content-Disposition header is ignored. · Issue #26053 · spring-projects/spring-framework
November 9, 2020 - I want to download file from server via WebClient and get body with filename, but Content-Disposition header is ignored. webClient.get() // some code .retrieve() .toEntity(Resource::class.java) ... spring-framework/spring-webflux/src/main/java/org/springframework/web/reactive/function/clie...
Author   spring-projects
Find elsewhere
🌐
GitHub
github.com › barlog-m › spring-webflux-file-upload-download-example
GitHub - barlog-m/spring-webflux-file-upload-download-example
April 21, 2023 - Spring WebFlux Upload/Download example · Contains: Controller with two endpoints: upload and download · Service with methods for save, load and check hash sum · Unit test for controller · Integration test for controller with examples, how to user WebClient for upload and download file ·
Starred by 11 users
Forked by 7 users
Languages   Kotlin 100.0% | Kotlin 100.0%
🌐
YouTube
youtube.com › gyan darpan
57 Learn File Download in Spring Boot RestTemplate & WebClient Guide - YouTube
Learn File Download in Spring Boot RestTemplate & WebClient GuideGitHub: https://github.com/gyandarpan22/Spring-Boot-Tutorial/tree/main/Learn File Downlo...
Published   December 22, 2024
Views   47
🌐
Stack Overflow
stackoverflow.com › questions › 68954525 › downloading-large-file-with-spring-webclient
kotlin - Downloading large file with spring webClient - Stack Overflow
August 27, 2021 - I decided to use spring webClient in order to get large files. Here is my method · suspend fun downloadDocument( documentId: UUID ): InputStream = webClient.get() .uri("<some uri>") .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_OCTET_STREAM_VALUE) .retrieve() .bodyToFlux<DataBuffer>() .awaitLast() .asInputStream(true)
🌐
BezKoder
bezkoder.com › home › spring webflux file download example
Spring WebFlux File download example - BezKoder
April 22, 2023 - Our Spring Boot Reactive Web – WebFlux File Upload Application will provide API for downloading File from server with the url link.
🌐
Oodlestechnologies
oodlestechnologies.com › blogs › how-to-download-a-file-directly-from-url-in-spring-boot
How To Download A File Directly From URL In Spring Boot
February 14, 2020 - In this blog, we will discuss how to download a file directly from Server using direct download link URL using StreamingResponseBody in Spring boot. To download a file Using stream by StreamingResponseBody is possible. Here server will write data to OutputStream and at the same time browser will read data without holding up the Servlet container thread.
🌐
Stack Overflow
stackoverflow.com › questions › 67884656 › how-to-download-large-file-using-webclient-spring
How to download large file using WebClient Spring? - Stack Overflow
June 8, 2021 - So I want to use file system to solve this. Currently i am using following piece of code to get all docs. public Documents getDoc() { WebClient webClient = webClientCreator.createWebClient(); return webClient .get() .uri(getTestURI()) .header(HttpHeaders.ACCEPT, Constants.ACCEPT_TYPE) .retrieve() .bodyToMono(Documents.class) .block(); }
🌐
Baeldung
baeldung.com › home › spring › spring webflux › upload a file with webclient
Upload a File with WebClient | Baeldung
July 24, 2025 - Our applications often have to handle file uploads via an HTTP request. Since Spring 5, we can now make these requests reactive. The added support for Reactive Programming allows us to work in a non-blocking way, using a small number of threads and Backpressure. In this article, we’ll use WebClient ...
🌐
Blogjava
blogjava.net › paulwong › archive › 2022 › 09 › 22 › 450822.html
Downloading Large Files using Spring WebClient - paulwong - BlogJava
September 22, 2022 - posted on 2022-09-22 13:14 paulwong 阅读(266) 评论(0) 编辑 收藏 所属分类: SPRING 、SPRING BOOT · Powered by: BlogJava Copyright © paulwong
🌐
Baeldung
baeldung.com › home › http client-side › download a large file through a spring resttemplate
Download a Large File Through a Spring RestTemplate | Baeldung
February 11, 2025 - In this tutorial, we’re going to show different techniques on how to download large files with RestTemplate. RestTemplate is a blocking and synchronous HTTP Client introduced in Spring 3. According to the Spring documentation, it’ll be deprecated in the future since they’ve introduced WebClient as a reactive nonblocking HTTP client in version 5.
🌐
Spring
spring.io › guides › gs › reactive-rest-service
Getting Started | Building a Reactive RESTful Web Service
You can use this pre-initialized project and click Generate to download a ZIP file.
🌐
Reddit
reddit.com › r/javahelp › downloading a file with webclient
r/javahelp on Reddit: Downloading a file with webclient
November 4, 2020 -

Hello everyone, I'm currently experimenting with the Spring webclient. I already successfully uploaded a file to another api with it, but I'm currently struggling to correctly download it.

Does anyone know a good resource(or can explain it themselves) that explains how I properly download a file with WebClient from another AP.

Top answer
1 of 1
1

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.