一种“unexpected end of stream”异常
此异常在使用OkHttp下载文件时出现
特点1:必现;
特点2:不是一开始出现,多出现在下载结尾处,并且每一次位置相同
原因:http响应报文中,header中含有“content-length”,并且它的值比实际文件大,哪怕几个byte也不行,就会报上面的错误,所以请服务器修正这个错误吧;
代码如下:
long read = source.read(sink, Math.min(bytesRemaining, byteCount));
if (read == -1) {
endOfInput(false); // The server didn't supply the promised content length.
throw new ProtocolException("unexpected end of stream");
}
当然unexpected end of stream
还有其他产生的原因,这里只是其中之一;
注:同样的链接,用浏览器下载也不能下载完成,所以请保证content-length
与实际大小保持一致;