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.
Answer from Andy Brown on Stack OverflowVideos
I am creating a huge byte[] with the size of the online file
Why? You don't need a byte array the size of the file. That just wastes space and adds latency. Just read and write buffers:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
This works for any byte[] buffer of size greater than zero. I generally use 8192.
With URLConnection you don't need the content-length either: just read to EOS as above.
There is no reason why you should use a DataInputStream at all. Use Files:
final Path dstFile = Paths.get("path/to/dstfile");
Files.copy(url.openStream(), dstFile);