1

I'm trying to download file of size > 2 GB (of .tar.gz format).

code:

try (InputStream fileInputStream = new FileInputStream(file)){
output = response.getOutputStream();
response.setContentType("application/gzip");
response.setContentLength((int) (file.length()));
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
IOUtils.copyLarge(fileInputStream, output);
output.flush();
if(fileInputStream != null) {
    fileInputStream.close();
   }
}


but getting below error.

2019-06-12 14:38:24,840 ERROR [https-jsse-nio-8543-exec-1]-rest.LogBundlesAPI: downloadLogBundle--> Exception occured while reading log bundle in download log bundle REST call.org.apache.catalina.connector.ClientAbortException: java.io.IOException: Connection reset by peer


Why above error & Is there any other way to download file of any size in Java?

Community
  • 1
  • 1
Ravi
  • 13
  • 5
  • Which type is your `response`? – Olivier Grégoire Jun 12 '19 at 08:58
  • 5
    Note that `int`s max positive value allows for a content length of no more than 2G – Gyro Gearless Jun 12 '19 at 08:58
  • If you are using tomcat - [check this setting](https://tomcat.apache.org/tomcat-9.0-doc/aio.html) `org.apache.tomcat.sendfile.support` – Victor Gubin Jun 12 '19 at 09:07
  • 2
    "java.io.IOException: Connection reset by peer" - the error suggests that your client code calling this download service closed the connection before the entire content was written. Please check the client code. Also if the client is browser this usually happens if the tab is closed before the content was written completely. In any case it reflects issue on the client side and not server side. – Shailendra Jun 12 '19 at 09:07
  • 1
    Note the restriction to 2G due to using `int` for the content-lenght is perhaps a design flaw in the servlet spec. You might try to bypass this restriction by directly sending the Content-length header - something along `response.setHeader("Content-length", file.length())` – Gyro Gearless Jun 12 '19 at 09:08
  • Which server are you deploying this code – TechFree Jun 12 '19 at 09:10

2 Answers2

1

Use Chunked transfer encoding. It is supported by most REST libraries (e.g. JAX-RS). I think you should consider using it.

jschnasse
  • 8,526
  • 6
  • 32
  • 72
0

You can use ftp protocol to download large files by dynamically creating ftp urls. Thanks