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

下载一个大文件(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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这个错误信息"org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 67,554; received: 7,619)"表示在发送HTTP请求给后端服务时,接收到的消息体长度与预期长度不一致,导致连接被提前关闭。 这个问题可能有多种原因导致,其中一个原因可能是后端服务的响应内容被截断了。根据引用和引用中的报错信息,这个错误在Jmeter压测过程中出现,可能是由于测试中发送的请求过于频繁,导致后端服务无法及时响应完成,从而截断了响应内容。 解决这个问题的一个方法是增加后端服务的处理能力,例如增加后端服务的线程数或者优化后端服务的性能。另外,引用提到可以通过设置Tomcat的配置文件server.xml来调整请求超时时间,这也是解决这个问题的一个方案。 需要注意的是,具体的解决方法可能因系统环境和具体的应用场景而有所不同,建议根据实际情况进行分析和调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body](https://blog.csdn.net/javajxz008/article/details/82684479)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited mess](https://blog.csdn.net/light2081/article/details/130914834)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值