spring boot 下载文件出错 org.apache.http.ConnectionClosedException: Premature end of Content-Length delimi

本文分享了在处理大文件(约100MB)下载过程中遇到的问题及解决方案,包括如何通过调整读写方式提高下载速度,从逐字节读取改为按块读取,并确保完整写入所有数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下载一个大文件(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;
}

最后,记录每次读取的长度,在写入时写入特定长度的字节,不多写,也不少写,解决问题。

 

小程序查看更多java相关面试题

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值