我有下面的代碼來下載文件形式URLHttpURLConnection的閱讀超時
HttpURLConnection urlConn = (HttpURLConnection)urlOfFile.openConnection();
urlConn.setConnectTimeout(5000);
urlConn.setReadTimeout(10000);
StatusInfo.fileSizeTobeDownload = urlConn.getContentLength();
InputStream reader = urlConn.getInputStream();
FileOutputStream writer = new FileOutputStream(downloadFolder+fileName);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) > 0) {
writer.write(buffer, 0, bytesRead);
buffer = new byte[1024];
StatusInfo.fileSizeDownloaded+=bytesRead;
}
writer.close();
reader.close();
此代碼工作正常,但有時我得到了以下錯誤:
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:146)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:554)
at sun.security.ssl.InputRecord.read(InputRecord.java:509)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:850)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:807)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:94)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
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:2582)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2577)
沒有任何網絡斷開。有沒有其他的方式來配置「ReadTimeout」。
我不確定,但可能是您的上網速度太低,或可能需要一些時間來回復 – Pragnani 2013-03-16 04:55:52
我們有很好的速度,我試圖從3-4臺機器同時下載它,並在一臺機器上失敗,並在每次重試時失敗,並在一段時間後成功下載。 – Naresh 2013-03-16 05:01:04