Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:大型文件的意外流结束错误_Java_Android_Fileinputstream_Fileoutputstream - Fatal编程技术网

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

Java:大型文件的意外流结束错误,java,android,fileinputstream,fileoutputstream,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

我正在尝试下载多个块中的单个文件。但对于大于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

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

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;
}

如果不指定范围或使用其他服务器,它是否有效?顺便说一句:你为什么要分块读取文件?没有尝试过没有范围,这里的主要目标是通过将一个文件分成多个块并使用不同的线程下载每个块来实现最大的下载速度。您已经比较了单线程下载的下载性能,并且它更快@AjmalRasi?多线程下载总是更快,这就是IDM所做的。从加速下载的意义上说,它更快。我的问题已解决:)问题出在服务器上:)如果不指定范围或使用其他服务器,它是否有效?顺便说一句:你为什么要分块读取文件?没有尝试过没有范围,这里的主要目标是通过将一个文件分成多个块并使用不同的线程下载每个块来实现最大的下载速度。您已经比较了单线程下载的下载性能,并且它更快@AjmalRasi?多线程下载总是更快,这就是IDM所做的。从加速下载的意义上说,它更快。我的问题已解决:)问题与服务器有关:)
@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;
}