try{
URLPath = "xxxxx";
URL url = new URL(URLPath);
uc = (HttpURLConnection) url.openConnection();
uc.setReadTimeout(30000);//timeout set
uc.connect();// connect
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);
}
in.close();
uc.disconnect();
fos.close();
}
这是我的下载代码。
我有问题,当我下载像600mb这样的小文件时,
并且没有错误发生
但我下载2G文件大小,错误发生了。
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(Unknown Source)
at java.io.BufferedInputStream.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 sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.close(Unknown Source)
at Download_File.downloadFile(Download_File.java:66)
at downloadFileFunctionThread.run(downloadFileFunctionThread.java:116)
这是错误消息,我谷歌的错误消息 我发现一些解决方案是插座关闭问题 但是下载大尺寸文件时发生了错误 所以我不知道如何解决这个问题 谢谢大家。
答案 0 :(得分:2)
可能与
有关uc.setReadTimeout(30000);//timeout set
对于较大的文件,您的连接可能会超时,提供的值高于下载文件所需的持续时间,或者不指定一个。