如何在使用 httpurlconnection 从 InputStream 读取时设置超时?
How to set a timeout while reading from InputStream using httpurlconnection?
我有一个 java 应用程序,我在其中使用 HttpURLConnection 下载文件。我在连接超时属性中设置了 15 秒,在读取超时属性中设置了 1 小时。据我了解,如果服务器上的文件大到需要超过 1 小时才能下载,它将因超时异常而失败。工作正常。问题是,当我在下载过程中(从 InputStream 读取缓冲区)从客户端机器上拔出 Internet 电缆时,下载过程不会立即终止,需要 1 小时(读取超时)才能中断下载。有什么办法可以终止下载过程?
以下是源代码:
public class HttpDownloadUtility {
private static final int BUFFER_SIZE = 4096;
public static void downloadFile(String fileURL, String saveDir)
throws IOException {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setConnectTimeout(15*1000);
httpConn.setReadTimeout(60*60*1000);
int responseCode = httpConn.getResponseCode();
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
} else {
// extracts file name from URL
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
fileURL.length());
}
System.out.println("Content-Type = " + contentType);
System.out.println("Content-Disposition = " + disposition);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);
// opens input stream from the HTTP connection
InputStream inputStream = httpConn.getInputStream();
String saveFilePath = saveDir + File.separator + fileName;
// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) { //This is where the download gets stuck on some connection issues
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("File downloaded");
} else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode);
}
httpConn.disconnect();
}
}
As per my understanding, if file on server is big enough to take more than 1 hour to download, it will fail with timeout exception.
没有。如果服务器超过一个小时未能响应任何单个读取请求,它将失败。当您调用 read()
时超时开始,并在收到对该读取的响应或超时期限到期时结束。然后它再次开始下一次阅读。总时间与它无关。你完全误会了。
Works fine.
工作正常,但与您描述的不一样。
The problem is, when I pull the Internet cable out from the client machine while download is in progress(reading buffer from InputStream), the download process does not terminates immediately, it takes 1 hour (read timeout) to break the download.
这正是它应该做的,如上所述。
Is there any way I can terminate the download process?
设置更短的超时时间。将它设置得足够长,以便服务器应在该间隔内响应,并且设置得足够短,电缆拉动不会花费太长时间超时。一个小时太长了。试试几分钟。
我有一个 java 应用程序,我在其中使用 HttpURLConnection 下载文件。我在连接超时属性中设置了 15 秒,在读取超时属性中设置了 1 小时。据我了解,如果服务器上的文件大到需要超过 1 小时才能下载,它将因超时异常而失败。工作正常。问题是,当我在下载过程中(从 InputStream 读取缓冲区)从客户端机器上拔出 Internet 电缆时,下载过程不会立即终止,需要 1 小时(读取超时)才能中断下载。有什么办法可以终止下载过程? 以下是源代码:
public class HttpDownloadUtility {
private static final int BUFFER_SIZE = 4096;
public static void downloadFile(String fileURL, String saveDir)
throws IOException {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setConnectTimeout(15*1000);
httpConn.setReadTimeout(60*60*1000);
int responseCode = httpConn.getResponseCode();
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
} else {
// extracts file name from URL
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
fileURL.length());
}
System.out.println("Content-Type = " + contentType);
System.out.println("Content-Disposition = " + disposition);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);
// opens input stream from the HTTP connection
InputStream inputStream = httpConn.getInputStream();
String saveFilePath = saveDir + File.separator + fileName;
// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) { //This is where the download gets stuck on some connection issues
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("File downloaded");
} else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode);
}
httpConn.disconnect();
}
}
As per my understanding, if file on server is big enough to take more than 1 hour to download, it will fail with timeout exception.
没有。如果服务器超过一个小时未能响应任何单个读取请求,它将失败。当您调用 read()
时超时开始,并在收到对该读取的响应或超时期限到期时结束。然后它再次开始下一次阅读。总时间与它无关。你完全误会了。
Works fine.
工作正常,但与您描述的不一样。
The problem is, when I pull the Internet cable out from the client machine while download is in progress(reading buffer from InputStream), the download process does not terminates immediately, it takes 1 hour (read timeout) to break the download.
这正是它应该做的,如上所述。
Is there any way I can terminate the download process?
设置更短的超时时间。将它设置得足够长,以便服务器应在该间隔内响应,并且设置得足够短,电缆拉动不会花费太长时间超时。一个小时太长了。试试几分钟。