gpt4 book ai didi

java - HttpURLConnection 读取超时

转载 作者:搜寻专家 更新时间:2023-11-01 03:40:36 27 4
gpt4 key购买 nike

我有以下代码来下载文件表单 URL

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”。

最佳答案

这个问题没有意义。你设置了一个读取超时,你得到了一个读取超时。如果你比你预期的要早得到它,把它设置得更长一些。您唯一需要的“配置读取超时的其他方法”是更改超时值。那应该是什么,只有您知道,因为只有您知道为什么要设置它。十秒对我来说确实太短了。

注意,您不需要继续重新创建读取缓冲区。你只是在制造成吨的垃圾。

关于java - HttpURLConnection 读取超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15445647/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com