Java Http 连接读取超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23422650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Http Connection Read Timeout
提问by user3595081
Dear Friends here i want the code to set the time for response from given URL in particular given seconds else it should be error
亲爱的朋友们,我希望代码设置来自给定 URL 的响应时间,特别是给定的秒数,否则它应该是错误的
Here i attached my code that having 730MB Iso file download page, its minimum takes 30min for me. Here I am giving just 10 seconds to download the file and getting the response from that page,I am sure 30min downloading file it wont give response in just 10 sec but here its giving proper response.
在这里我附上了我的代码,它有 730MB 的 Iso 文件下载页面,对我来说最少需要 30 分钟。在这里,我只给了 10 秒的时间来下载文件并从该页面获得响应,我确信 30 分钟的下载文件不会在 10 秒内给出响应,但在这里它给出了正确的响应。
please check my code here and tel me where i did mistake..
请在此处检查我的代码并告诉我我做错的地方..
public class test {
public static void main(String[] args) throws ClientProtocolException, IOException {
// TODO Auto-generated method stub
nodeBIT("");
}
private static void nodeBIT(String string) throws ClientProtocolException, IOException {
String url1 = "http://download.microsoft.com/download/e/e/9/ee9d0116-c9fe-4fc2-b59c-406cbfb6d515/xpsp3_5512.080413-2113_usa_x86fre_spcd.iso";
URL obj = new URL(url1); URLConnection con = obj.openConnection();
con.setConnectTimeout(10000);
con.setReadTimeout(10000); // 10 seconds to read the file
InputStreamReader input = new InputStreamReader(con.getInputStream());
BufferedReader in = new BufferedReader(input);
String inputLine;
String fullline = "";
while ((inputLine = in.readLine()) != null)
{
fullline = fullline.concat(inputLine);
}
System.out.println(fullline);
}
}
采纳答案by Scary Wombat
as long as data is being received it will not timeout as per javadocs
只要接收到数据,它就不会按照javadocs超时
You could modify your loop
你可以修改你的循环
long se = System.currentTimeMillis();
while ((inputLine = in.readLine()) != null)
{
fullline = fullline.concat(inputLine);
if ( System.currentTimeMillis() > se + 10000) {
throw new IOException ();
}
}
回答by Praba
The APIsays that
该API表示,
"if the timeout expires before there is data available for read"
“如果在有数据可供读取之前超时到期”
which means that once the data becomes available for download, then the connection will not timeout. If your server takes more than 10 seconds to even start sending the data back to the client, that's when timeout kicks in.
这意味着一旦数据可供下载,连接就不会超时。如果您的服务器甚至需要 10 秒以上的时间才开始将数据发送回客户端,那么超时就会开始。
回答by MacDaddy
Check the time-out on the server too. Because when you trying to download large file through JAVA some time server is giving time out exception.
检查服务器上的超时。因为当您尝试通过 JAVA 下载大文件时,有时服务器会发出超时异常。
here are some other example code for downloading data from server to app using FTP.here i used two list remoteFileList,localFileList to give sourse file and local file paths.
这里有一些其他示例代码,用于使用 FTP 从服务器下载数据到应用程序。这里我使用了两个列表 remoteFileList,localFileList 来提供源文件和本地文件路径。
FTPClient ftpClient = new FTPClient();
ftpClient.connect(Constant.FTP_SERVER_IP);
ftpClient.login(Constant.FTP_USER_NAME, Constant.FTP_PASSWORD);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
System.out.println("Connection Succeeded");
for (int i = 0; i < remoteFileList.size(); i++) {
remoteFileName = remoteFileList.get(i);
localFileName = localFileList.get(i);
File localFile = new File(localFileName);
if (localFile.exists()) {
System.out.println("File Already Exists!!");
} else {
downloadFile = new FileOutputStream(localFile);
boolean isDownloaded = ftpClient.retrieveFile(remoteFileName, downloadFile);
System.out.println("Downloaded");
if (isDownloaded) {
System.out.println(" - Success.\n");
} else {
System.out.println( " - Unsuccess.\n");
}
}
if (i == remoteFileList.size() - 1 && this.DownloadType == 1) {
System.out.println("This is time to open !");
JOptionPane.showMessageDialog(this, "Image downloading completed.Please open the inspection");
}
}
} else {
ftpClient.disconnect();
}