简体   繁体   中英

Needs to download big file in chunks

I have 20 MB of the file. I just want to download the file in the browser in chunks/streams so that If there is another request it is not blocked for a long period of time.

I also see the pause behavior in a long file. It is not needed right now. If someone can explain this because of my curiosity to know how it works.

I have the file saved in DB. When I get the request to download the file. I will get the file from DB. I push the byte code in the response body and send it to the browser. In the browser, I have created one link and download the file in one go.

Back-end code:

  @GetMapping("/job-detail/{jobExecutionId}/file-detail")
  public ResponseEntity<Resource> getFileDetail(@PathVariable final Long jobExecutionId) {
    final FileDetailDto fileDetailDto =
        fileTaskletService.getFileDetailByExecutionId(jobExecutionId);

    return ResponseEntity.ok().contentType(new MediaType("text", "csv"))
        .header(HttpHeaders.CONTENT_DISPOSITION,
            "attachment; filename=\"" + fileDetailDto.getFileName() + "\"")
        .body(new ByteArrayResource(fileDetailDto.getData()));
  }

I wants to download the file in chunks so that other requests can also start the downloading on the same time.

From the wording of your question it sounds like you want the server framework to set the following header for you so that the data flows back in chunked blocks:

Transfer-Encoding: chunked

To do that you need to supply a Spring resource where the full size is not known in advance. You have used ByteArrayResource where the full size is known and results in a Content-Length header being set in the response and chunked is not used.

Change your code to use InputStreamResource and the service will stream the response back to the client with a chunked transfer encoding and no content length. Here's a sample (syntax unchecked):

try(ByteArrayInputStream bis = new ByteArrayInputStream(fileDetailDto.getData())) {
  return ResponseEntity.ok().contentType(new MediaType("text", "csv"))
          .header(HttpHeaders.CONTENT_DISPOSITION,
              "attachment; filename=\"" + fileDetailDto.getFileName() + "\"")
          .body(new InputStreamResource(bis));
}

While this will get you a chunked response I'm not convinced it's the root of your problems with the browser because they are all very capable of asynchronously streaming back data regardless of how the server provides it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM