0
      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)

これはエラーメッセージです、私はエラーメッセージをグーグルで検索します。
いくつかの解決策はソケットクローズの問題だと思います。
しかし、大きなサイズのファイルをダウンロードするとエラーが発生しました。
だから私は問題を解決する方法がわかりません。
皆さんありがとう。

4

1 に答える 1

2

おそらく問題は

uc.setReadTimeout(30000);//timeout set

より大きなファイルの場合、接続がタイムアウトになる可能性があります。ファイルのダウンロードに必要な期間よりも高い値を指定するか、指定しないでください。

于 2013-01-17T07:22:47.217 に答える