Workaround for using Taroutputstream to appear request to write ' 1024x768 ' bytes exceeds size in header error

Source: Internet
Author: User

Because in the test process, the client will determine the value of each parameter in the request sent to Server B according to the response returned by server A, so it is now necessary to simulate the server's response. And this Project Server A's response returns a stream, a gzip compressed format stream, which compresses multiple files, so you need to write the appropriate groovy script. I'm using Apache's Ant package here. But it went wrong when it was running. Error hints are as follows

Caught:java.io.IOException:request to write ' 1024x768 ' bytes exceeds size in header of ' 29886 ' bytes for entry ' Rate.csv '
Java.io.IOException:request to write ' 1024x768 ' 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).

Literally, then carefully read the API document read (byte[] b), read (byte[] b,int off, int len), write (byte[] wbuf), write (byte[] wbuf, int woffset, in T numtowrite) After the relevant explanation, after trying, finally solved. It seems that I used to read the file and write to the file using a method of a parameter is not good habits, although there was no problem, but this time, and then the use of Read (byte[] b,int off, int len) and write (byte[] wbuf, int woffset , int numtowrite), so that this error can be prevented from happening again.

Attached code

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 ();

Workaround for using Taroutputstream to appear request to write ' 1024x768 ' bytes exceeds size in header error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.