okhttp断点下载unexpected end of stream异常解决

    xiaoxiao2023-11-01  182

    try (FileOutputStream fileOutputStream = new FileOutputStream(file, true); InputStream is = response.body().byteStream()) { while ((len = is.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, len); downloadLength += len; downloadInfo.setProgress(downloadLength); e.onNext(downloadInfo); } fileOutputStream.flush(); }

     

    解决方法:

    try (FileOutputStream fileOutputStream = new FileOutputStream(file, true); InputStream is = response.body().byteStream()) { while ((downloadLength != contentLength) && (len = is.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, len); downloadLength += len; downloadInfo.setProgress(downloadLength); e.onNext(downloadInfo); } fileOutputStream.flush(); }

    downloadLength:已下载长度

    contentLength:文件总长度

    最新回复(0)