因为测试流程中,所测客户端会根据服务器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. 获取iframe的元素并进行操作

    获取iframe中的document元素有一下集中方法: 1.getElementById()方法和contentWindow属性: window.onload=function(){ /*必须等待页 ...

  2. Codeforces 687C The Values You Can Make(DP)

    题目大概说给n个各有价值的硬币,要从它们中选出若干个组合成面值k,而要求的是各个方案里这些选出的硬币能组合出来的面值有哪些. 有点绕.. dp[i][j][k]表示前i个硬币中 能否 组合成面值j且选 ...

  3. git的合并与推送

    集中式合作模式 1.git fetch 获取远程更新 2.git merge origin/master 进行合并,如果报错,则相应解决.注:你得用git bash命令行执行才能看见报错详情,用ecl ...

  4. ural 1144. The Emperor's Riddle

    1144. The Emperor's Riddle Time limit: 1.0 secondMemory limit: 4 MB Background In the olden times th ...

  5. SQL: See the TSQL underneath the sp_execute calls

    When use SQL Server Profiler, on the Events Selection tab, check Show all events; Find the Store Pro ...

  6. ccc 调试方法

    当修改完一个函数,但是不知道哪个函数调用的时候没有传递正确的参数的时候 需要找出调用这个函数的所有语句,于是我注释掉这个函数就可以了

  7. Hadoop中客户端和服务器端的方法调用过程

    1.Java动态代理实例 Java 动态代理一个简单的demo:(用以对比Hadoop中的动态代理) Hello接口: public interface Hello { void sayHello(S ...

  8. 优化特性(Attribute)性能

    通过这篇文章,不仅可以了解到Attribute的工作原理,还可以了解到GetcustomeAttribute是的内部执行流程.最后,你会看到,使用缓存机制可以极大的优化反射Attribute的性能. ...

  9. CF 66D. Petya and His Friends

    题目链接 java的水题,特判啊...依旧无法1Y. import java.util.*; import java.math.*; public class Main { public static ...

  10. 【noiOJ】p8206

    02:二分法求函数的零点 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 有函数: f(x) = x5 - 15 * x4+ 85 * x3- 225 * x ...