因为测试流程中,所测客户端会根据服务器A返回的response决定发送给服务器B的请求里各参数的值,所以现在需要模拟服务器的响应。而这个项目服务器A的响应式返回一个流,一个GZIP压缩格式流,压缩的是多个文件,所以需要编写相应的groovy脚本。我这里使用了apache的ant包。不过在运行的时候出错了。错误提示如下

Caught: java.io.IOException: request to write '1024' bytes exceeds size in header of '29886' bytes for entry 'rate.csv'
java.io.IOException: request to write '1024' bytes exceeds size in header of '29886' bytes for entry 'rate.csv'
at org.apache.tools.tar.TarOutputStream.write(TarOutputStream.java:279)
at org.apache.tools.tar.TarOutputStream.write(TarOutputStream.java:260)
at org.apache.tools.tar.TarOutputStream$write.call(Unknown Source)
at temp.packFile(temp.groovy:26)
at temp.process(temp.groovy:51)
at temp.run(temp.groovy:55)。

按照字面意思,然后仔细看了下api文档read(byte[] b), read(byte[] b,int off, int len), write(byte[] wBuf), write(byte[] wBuf, int wOffset, int numToWrite)的相关解释后, 经过尝试,终于解决了。看来我以前读取文件和写入文件使用一个参数的方法的习惯不好,虽然之前都没有出问题,但是这次就出现了,以后还是使用read(byte[] b,int off, int len)和write(byte[] wBuf, int wOffset, int numToWrite),这样就能避免这个错误的再次发生了。

附上代码

import java.util.zip.GZIPOutputStream
import org.apache.tools.tar.TarEntry
import org.apache.tools.tar.TarOutputStream

public void packFile(String... filePath){
int num=filePath.length;
String targetPath=filePath[num-1]
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(targetPath));
TarOutputStream tos=new TarOutputStream(bos);
tos.setLongFileMode(TarOutputStream.LONGFILE_GNU);
for(int i=0;i<num-1;i++){
File sourceFile=new File(filePath[i]);
TarEntry te=new TarEntry(sourceFile.getName());
te.setSize(sourceFile.length());
println(sourceFile.length())
tos.putNextEntry(te);
byte[] buffer=new byte[1024];
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(sourceFile));
int count=0
while((count=bis.read(buffer,0,1024))>-1){
tos.write(buffer,0,count);
} 
tos.closeEntry();
tos.flush();
bis.close();
}
tos.close();
}

public void compressFile(String filePath){
FileInputStream fis=new FileInputStream(filePath);
GZIPOutputStream gos=new GZIPOutputStream(new FileOutputStream(filePath+".gz"));
byte[] buffer=new byte[1024];
while(fis.read(buffer)>-1){
gos.write(buffer);
}
fis.close();
gos.finish();
gos.close();
}

public void process() {
String sourcePath1="D:/rate.csv";
String sourcePath2="D:/plan.csv";
String tarPath="D:/test/test.tar";
packFile(sourcePath1,sourcePath2,tarPath);
compressFile(tarPath);
}

process();

使用TarOutputStream出现 request to write '1024' bytes exceeds size in header错误的解决方法的更多相关文章

  1. Got a packet bigger than‘max_allowed_packet’bytes错误的解决方法

    通常项目上线前都有一些初始化数据需要导入,在今天博客系统发布前我使用sqlyog工具远程登录服务器的Mysql数据库,执行sql脚本对初始数据进行导入的时候报错: Got a packet bigge ...

  2. .NET上传大文件时提示Maximum request length exceeded错误的解决方法

    使用IIS托管应用程序时,当我们需要上传大文件(4MB以上)时,应用程序会提示Maximum request length exceeded的错误信息.该错误信息的翻译:超过最大请求长度. 解决方法: ...

  3. 因用了NeatUpload大文件上传控件而导致Nonfile portion > 4194304 bytes错误的解决方法

    今天遇到一个问题,就是“NeatUpload大文件上传控件而导致Nonfile portion > 4194304 bytes错误”,百度后发现了一个解决方法,跟大家分享下: NeatUploa ...

  4. 今天发现猎豹浏览器的一个大坑 Request.IsAuthenticated 一直为 false;另外附加原因以及临时的解决方法

    今天掉到了一个大坑里面,爬了1个多小时才发现不是代码的问题,居然是浏览器的问题… 下面是问题的发生过程 单点登陆  有2个站点  http://a.abc.com  http://b.abc.com ...

  5. Request method 'POST' not supported错误和解决方法

    在使用SpringBoot的时候,在html页面用form表单post提交数据的时候报错: Request method 'POST' not supported 错误解析: 我是用的前端页面是HTM ...

  6. 网页出现400 Bad Request Request Header Or Cookie Too Large错误的解决方法

    在开发项目过程中,突然遇到400 Bad Request Request Header Or Cookie Too Large的报错,我也是第一次出现这样的错误,感觉还是挺新奇的. 分析下出现错误的原 ...

  7. MySQL导入数据报 Got a packet bigger than‘max_allowed_packet’bytes 错误的解决方法

    MySQL根据配置文件会限制Server接受的数据包大小.有时候大的插入和更新会受 max_allowed_packet 参数限制,导致大数据写入或者更新失败. 通过终端进入mysql控制台,输入如下 ...

  8. nginx过一段时间出现400 Bad Request 错误的解决方法

    tomcat整合nginx成功后,等访问一段时间后,会出现 Bad Request (Invalid Hostname)的错误, 因为是已经成功的配置,所以判定可能是哪里的限制设置有问题,最后在官方网 ...

  9. 【Azure 应用服务】PHP应用部署在App Service for Linux环境中,上传文件大于1MB时,遇见了413 Request Entity Too Large 错误的解决方法

    问题描述 在PHP项目部署在App Service后,上传文件如果大于1MB就会遇见 413 Request Entity Too Large 的问题. 问题解决 目前这个问题,首先需要分析应用所在的 ...

随机推荐

  1. BestCoder Round #72 (div.2)

    后面的题目补不懂了 暴力 1001 Clarke and chemistry 这题也把我搞死了..枚举系数判断就行了 #include <cstdio> #include <algo ...

  2. web api :Routing in ASP.NET Web API

    引 Web API 和SignalR都是在服务层. If you are familiar with ASP.NET MVC, Web API routing is very similar to M ...

  3. JavaScript 全局对象

    全局属性和函数可用于所有内建的 JavaScript 对象. 顶层函数(全局函数) 函数 描述 decodeURI() 解码某个编码的 URI. decodeURIComponent() 解码一个编码 ...

  4. HTML图片元素(标记)

    <html> <head> <title>第一个网页</title> </head> <body> ************** ...

  5. Parameter of Revit API – 19: Add FamilyParameter (FamilyManager.AddParameter)

      New family parameters (FamilyParameter) can be added to a family document through the FamilyManage ...

  6. 几个Windows电脑小技巧

    1. 为cmd命令提示符设置默认的初始路径: 到开始菜单-附件-属性  里面有起始位置选项 其中%HOMEDRIVE%%HOMEPATH%就代表起始位置 如想每次键cmd进入命令提示符后的初始位置是 ...

  7. 【转】SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法

    转自:http://www.coderli.com/junit-spring-test-applicationcontext JUnit单元测试用例中使用Spring框架,直接方式如下. @RunWi ...

  8. ACM 擅长排列的小明

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...

  9. 【BZOJ2473/2120】维护队列 分块+二分

    Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会 ...

  10. 你也可以用java的swing可以做出这么炫的mp3播放器_源码下载

    I had published the blog : 你用java的swing可以做出这么炫的mp3播放器吗? and to display some screenshots about this M ...