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:流關閉
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)
這是錯誤信息,我google錯誤信息。
我發現一些解決方案是套接字關閉問題。
但下載大文件時發生錯誤。
所以我不知道如何解決這個問題。
謝謝大家。
「當我下載一個像600mb這樣的小文件時」是什麼。你確定你輸入正確嗎? – Zyerah
文件大小爲600MB – MagicConch