How to avoid byte[] for download of 4GB file

55 views Asked by At

I'm having problems downloading 4GB files with Spring Boot, because the byte array can't contain them (max 2.1GB).

I found various examples online on how to stream files, but I can't apply them to my specific case and would like advice on the way to follow.

In my case I receive a call from the Angular front-end telling me which file (4GB) I have to take from an external rest service (my current problem is here), and then I have to send it as a response to the Angular front-end (even zipped would be fine).

I currently do this when I call the external service. All work fine with small and medium file, but I get an error when the bytes exceed 2.1GB. The error clearly says that a byte array has a maximum size of 2.1GB.

WebClient wClient = buildWebClient(c.get(END_POINT));

byte[] file = wClient.get().uri("/v2/nodes/" + fileId + "/content").header("Ticket", ticket).exchange()
                    .flatMap(response -> response.bodyToMono(ByteArrayResource.class))
                    .map(ByteArrayResource::getByteArray).block();

public WebClient buildWebClient(String endpoint) {
    return WebClient.builder().baseUrl(endpoint).defaultCookie("cookieKey", "cookieValue")
            .codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1))
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .defaultUriVariables(Collections.singletonMap("url", endpoint)).build();
}

I would like to find a solution that will allow me to do this avoiding the byte array, does anyone have any suggestions, probably using streams?

0

There are 0 answers