我正在尝试在非常慢的连接中以以下方式下载文件:
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.");
我注意到在下载时总是在180秒后中断,可能是因为本机Windows套接字超时原因。设置setConnectTimeout(...)或setReadTimeout(...)的超时时间并不能解决问题。
我尝试使用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
完整文件在20分钟后成功保存在磁盘上。
HttpURLConnection有什么问题?
编辑: 我尝试通过HTTP协议从其他服务器下载测试文件,一切正常。看起来是一个特定于服务器或协议的问题。但是为什么wget可以下载整个文件呢?
编辑2:根据您的建议,我还尝试了以下操作:
- 删除connection.setDoOutput(true);
- 使用直接链接以避免重定向302
- 使用自定义实现替换Files.copy方法
不幸的是,以上操作都没有帮助。
编辑3: 我注意到文件也可以通过不安全的HTTP协议在同一台服务器上获得。所以我只改变了代码中的URL,在120秒后得到了结果:
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)