I'm trying to download a file on very slow connection in this way:
java.net.URL url = new URL("https://X.X.X.X:8443/path/2f6b88cf2b70ee933197edfc9627a9bc/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setConnectTimeout(240 * 1000);
connection.setReadTimeout(240 * 1000);
long start = System.currentTimeMillis();
Files.copy(connection.getInputStream(), new File("test.zip").toPath());
System.out.println("Time: "+((System.currentTimeMillis() - start) / 1000) + " sec.");
and I noticed that for some reasons (native windows socket timeout?) it always breaks after 180 seconds of downloading without any exceptions.
Timeout set in setConnectTimeout(...) or setReadTimeout(...) doesn't help.
I tried to download that file using wget:
wget https://X.X.X.X:8443/path/2f6b88cf2b70ee933197edfc9627a9bc/ --no-check-certificate
--2015-09-07 14:36:12-- https://X.X.X.X:8443/path/2f6b88cf2b70ee933197edfc9627a9bc/
Connecting to X.X.X.X:8443... connected.
WARNING: The certificate of ‘X.X.X.X’ is not trusted.
WARNING: The certificate of ‘X.X.X.X’ hasn't got a known issuer.
The certificate's owner does not match hostname ‘X.X.X.X’
HTTP request sent, awaiting response... 302 Found
Location: https://X.X.X.X:8443/files/test.zip [following]
--2015-09-07 14:36:16-- https://X.X.X.X:8443/files/test.zip
Reusing existing connection to X.X.X.X:8443.
HTTP request sent, awaiting response... 200 OK
Length: 321917584 (307M) [application/zip]
Saving to: ‘test.zip’
test.zip 100%[====================================================================================>] 307.00M 253KB/s in 19m 50ss
Complete file was successfully saved on disk after 20 minutes.
What is wrong with HttpURLConnection?
Edit: I tried to download test file from other server by http protocol and everything was OK. It seems to be a server or protocol specific problem. But why wget manages to download whole file?
Edit2: following your advices I also tried:
Unfortunately none of above helps.
Edit3: I noticed that file is also available on the same server via unsecure http protocol. So I changed only the URL in my code and after 120 seconds I got:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:273)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at sun.net.www.MeteredStream.read(MeteredStream.java:134)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3066)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3060)
at java.nio.file.Files.copy(Files.java:2735)
at java.nio.file.Files.copy(Files.java:2854)
I finally found a solution here It is not possible to download large files at Jetty server (thanks again StackOverflow). Problem was on the server side.
Jetty 9.2 which we used has a bug that interrupts serving large files on slow connections (https://bugs.eclipse.org/bugs/show_bug.cgi?id=472621). It seems that exception is not always thrown.
Wget and browsers somehow were able to finish downloading despite stalled transfer or connection reset. Unfortunately my Java application was more sensitive...
Upgrade bundled Jetty to the last stable version 9.3.3 fixed all problems with downloads.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With