下载一个大文件(100M左右)报错,
org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 103767; received: 95040
百度 google 了很久尝试了很多办法,比如增加 http 链接的超时时间,增加缓冲区等都没有解决
最后发现,是我代码写错了
错误代码如下 本例中的伪代码没有输入输出流的关闭流程,实际代码中需要注意
public ZipOutputStream test(String zifDir,List<ZipFileItem> items) throws Exception{ String filePath = zifDir + UUID.randomUUID().toString()+".zip"; FileOutputStream fileOutputStream = new FileOutputStream(filePath); ZipOutputStream zip = new ZipOutputStream(fileOutputStream); int temp = -1; for (ZipFileItem item : items) { try { ZipEntry entry = new ZipEntry(item.getName()); zip.putNextEntry(entry); while((temp = item.getIs().read()) != -1){ // 主要错误在这里 zip.write(temp); } item.getIs().close(); }catch (Exception e){ e.printStackTrace(); } } return zip; }
其实代码本身没有错误,主要问题是这里我是一个字节一个字节的读取,并且写入输入流中,一旦文件过大,导致写入速度太慢,即使宽度足够,下载速度也不会超过1M/s,并且会报以上错误。
修改后代码:
public ZipOutputStream test(String zifDir,List<ZipFileItem> items) throws Exception{ String filePath = zifDir + UUID.randomUUID().toString()+".zip"; FileOutputStream fileOutputStream = new FileOutputStream(filePath); ZipOutputStream zip = new ZipOutputStream(fileOutputStream); byte [] bytes = new [1024]; // for (ZipFileItem item : items) { try { ZipEntry entry = new ZipEntry(item.getName()); zip.putNextEntry(entry); while((item.getIs().read(bytes)) != -1){ zip.write(bytes); // 这里有bug } item.getIs().close(); }catch (Exception e){ e.printStackTrace(); } } return zip; }
一次读取 1024 字节,并且写入输入流,极大的提高了速度,并且下载速度也能提高到 4-5 M/S (宽度速度足够的情况下)
但是这里还有一个问题,我下载的内容是图片,下载下来的图片全都不能用,显示不全,部分内容丢失。
主要问题是在写入的时候也是一次性写入 1024 字节内容,不管读取了多少,所以当一个文件的大小不是正好 1024 的整数倍的时候。就会出现以上问题。
最后修改代码为:
public ZipOutputStream test(String zifDir,List<ZipFileItem> items) throws Exception{ String filePath = zifDir + UUID.randomUUID().toString()+".zip"; FileOutputStream fileOutputStream = new FileOutputStream(filePath); ZipOutputStream zip = new ZipOutputStream(fileOutputStream); int readCount = -1; byte [] bytes = new [1024]; // for (ZipFileItem item : items) { try { ZipEntry entry = new ZipEntry(item.getName()); zip.putNextEntry(entry); while((readCount = item.getIs().read(bytes)) != -1){ zip.write(bytes,0,readCount); // 写入所有读取到的内容长度。 } item.getIs().close(); }catch (Exception e){ e.printStackTrace(); } } return zip; }
最后,记录每次读取的长度,在写入时写入特定长度的字节,不多写,也不少写,解决问题。