HttpURLConnection uc = null ;
URLPath = "";
URL url = new URL(URLPath);
uc = (HttpURLConnection) url.openConnection();
uc.setReadTimeout(10000);
uc.connect();
FileOutputStream fos = new FileOutputStream(savePath);
InputStream in = uc.getInputStream();
byte[] buffer = new byte[1024];
int Length = 0;
long FinishedLenth = 0;
while((Length = in.read(buffer)) > 0) {
FinishedLenth = FinishedLenth + Length;
fos.write(buffer, 0, Length);
}
uc.disconnect();
in.close();
fos.close();
这是我的功能代码,如果网络断开,
代码将出现异常。
我将重试函数写入异常。
userValue = 0,表示重试,1 表示取消。
int userValue = gm.GetIt("Error");
if(userValue == 0){
gp.interrupt();
uc.disconnect();
downloadFile(local,nas,ip,id,pw,fileId,index);
}
else{
conf.downloadState.get(index).setpercent("fail");
return false;
}
你好,我有麻烦。
我写代码,功能是下载。
如果网络断开,用户可以选择重试或取消。
但我收到错误消息。
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.net.www.MeteredStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
at Download_File.downloadFile(Download_File.java:54)
at downloadFileFunctionThread.run(downloadFileFunctionThread.java:113)
第 54 行是 while(......)。