有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试下载多个块中的单个文件。但对于大于20MB的文件,出现意外的流结束错误。 这种情况大约发生在下载量的60%-90%之间

W/System.err: java.net.ProtocolException: unexpected end of stream
W/System.err:     at com.安卓.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:421)
W/System.err:     at com.安卓.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) 个答案