Java:大文件意外结束流错误

时间:2017-03-31 19:45:41

标签: java android fileinputstream fileoutputstream

我试图以多个块的形式下载单个文件。但是对于大于20MB的文件,会出现意外的流错误。 这大概发生在下载的60%到90%之间。

W/System.err: java.net.ProtocolException: unexpected end of stream
W/System.err:     at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:421)
W/System.err:     at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:371)
W/System.err:     at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
W/System.err:     at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
W/System.err:     at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
W/System.err:     at java.io.FilterInputStream.read(FilterInputStream.java:107)
W/System.err:     at com.golshadi.majid.core.chunkWorker.AsyncWorker.run(AsyncWorker.java:94)

这行代码从服务器读取文件

AsyncWorker.java 行号 93 - 97

int len;
while (!interrupt && (len = remoteFileIn.read(buffer)) != -1) {
    chunkFile.write(buffer, 0, len);
    process(len);
}

这里我使用此代码设置内容长度范围。我想知道这是否与此错误有关。

connection.setRequestProperty("Range", "bytes=" + chunk.begin + "-" + chunk.end)

任何帮助将不胜感激

AysncWorker.java

@Override
public void run() {
    URL url=null;
    HttpURLConnection connection=null;
    InputStream remoteFileIn =null;
    FileOutputStream chunkFile;
    File cf;
    try {

        url = new URL(task.url);
        connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(0);
        connection.setReadTimeout(0);
        chunkSize = chunk.end-chunk.begin;
        if (chunk.end != 0) // support unresumable links
        {
            connection.setRequestProperty("Range", "bytes=" + chunk.begin + "-" + chunk.end);
           // connection.setFixedLengthStreamingMode((long) chunkSize);
            //connection.setChunkedStreamingMode(1024);
        }
        connection.connect();

        cf = new File(FileUtils.address(task.save_address, String.valueOf(chunk.id)));
        // Check response code first to avoid error stream
        int status = connection.getResponseCode();
          if(status == 416)
              remoteFileIn = connection.getErrorStream();
          else
              remoteFileIn = connection.getInputStream();

        //outputstream
        chunkFile = new FileOutputStream(cf, true);
        // set watchDoger to stop thread after 60 sec if no connection lost
        watchDog = new ConnectionWatchDog(60000, this);
        watchDog.start();

        int len;
        while (!interrupt && (len = remoteFileIn.read(buffer)) != -1) {
            watchDog.reset();
                chunkFile.write(buffer, 0, len);
                process(len);
        }

        chunkFile.flush();
        chunkFile.close();
        watchDog.interrupt();
        connection.disconnect();

        if (!interrupt) {
            observer.rebuild(chunk);
        }

    }catch (SocketTimeoutException e) {
        e.printStackTrace();

        observer.connectionLost(task.id);
        //pausing
        puaseRelatedTask();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        if (connection!=null){
            connection.disconnect();
        }
        if (remoteFileIn!=null){
            try {
                remoteFileIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(remoteFileIn!=null){
            try {
                remoteFileIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return;
}

0 个答案:

没有答案