Web projects sometimes experience the need to dynamically generate zip and download, but we do not want to generate redundant temporary files when generating zip files. Below will
Introduce my way of handling. There are improper places also hope that everyone criticized the advice.
This requirement is described as follows:
1. Generate XML files dynamically based on data
2. Package a dynamically generated XML file (one or more) into a zip file
3. After the customer clicks through the browser to complete the zip file downloading
The following starts to develop the corresponding function
1. Generate XML files dynamically based on data
This uses Dom4j-1.6.1.jar to implement XML read and create:
public class xmlhandledom4jhelper{/** * Creating a new XML document * * @author HLI/public static document
Generatedocument () {Document document = Documenthelper.createdocument ();
Document.addcomment ("Create XML");
Element root = document.addelement ("root");
Element Author1 = root.addelement ("author"). AddAttribute ("name", "Li Hong"). AddAttribute ("Age", "a"). addtext ("Li Hong");
Element Author2 = root.addelement ("info"). AddAttribute ("QQ", "317962765"). AddText ("317962765");
return document; /** * Writing a document to a file * * @param document * @throws IOException/public static void writ E (Bytearrayoutputstream bytearrayoutputstream,document Document) throws IOException {OutputFormat format = Outputforma
T.createprettyprint ();
Format.settrimtext (TRUE);
Format.setnewlineafterdeclaration (FALSE);
XMLWriter writer = new XMLWriter (bytearrayoutputstream, format);
Writer.write (document);
Writer.flush ();
Writer.close ();
}
}
2. Dynamically generate ZIP files
/** * Generate ZIP file (no temporary files generated) * * @param out * @author HLI * @return byte[] ZIP file byte array/public byte[] Generateco
Mmontranszipstreamnotempfile () {byte[] zipdata = null;
try {list<bytearrayoutputstream> xmlfilelistbyteoutputstream = new arraylist<bytearrayoutputstream> ();
Bytearrayoutputstream xmlfilebytearrayoutputstream = null;
for (int num = 1; num < 3; num++) {xmlfilebytearrayoutputstream = new Bytearrayoutputstream ();
Document document = Xmlhandledom4jhelper.generatedocument ();
Xmlhandledom4jhelper.write (xmlfilebytearrayoutputstream,document);
Xmlfilelistbyteoutputstream.add (Xmlfilebytearrayoutputstream);
} bytearrayoutputstream outputstreambyte = new Bytearrayoutputstream ();
----Compressed file://FileOutputStream F = new FileOutputStream ("F:\\report\\abc.zip"); Creates an output stream using the specified checksum checkedoutputstream csum = new Checkedoutputstream (Outputstreambyte, New CRC32 ());
Zipoutputstream Zos = new Zipoutputstream (csum);
Zos.setencoding ("GBK"); for (int i = 0; i < xmlfilelistbyteoutputstream.size (); i++) {Bytearrayoutputstream ByteStream = Xmlfilel
Istbyteoutputstream.get (i);
byte[] bytes = Bytestream.tobytearray ();
Bytearrayinputstream Byinputstream = new Bytearrayinputstream (bytes);
Begins writing a new zip file entry and navigates the stream to the beginning of the entry data zipentry zipentry = new ZipEntry ("Test_xml_file_" + i);
Zos.putnextentry (ZipEntry);
byte[] buffer = new byte[1024];
int readcount = byinputstream.read (buffer);
while (Readcount!=-1) {zos.write (buffer, 0, readcount);
Readcount = byinputstream.read (buffer);
}//Note, in the use of buffer flow to write compressed files, a condition must be refreshed after a certain, or may have some content will be deposited into the back of the entry to the Zos.flush ();
Close Byinputstream.close () after reading the document;
Zos.closeentry ()//Must be close here, otherwise download zip error zos.close ();
Zipdata = Outputstreambyte.tobytearray (); catch (Exception e) {E.printstaCktrace ();
return zipdata; }
3. After the customer clicks through the browser to complete the zip file downloading
Use the Struts2 file download feature here
Note: Refer to my other article: Click on the Open link
A> Code in Action:
Public InputStream Getdownzipstream () {
byte[] Zipdata = Reportservice.generatecommontranszipstreamnotempfile ();
Here for the downloaded file name
//If Chinese, you need to encode
downfilename = "Downzip.zip";
return new Bytearrayinputstream (Zipdata);
}
B>STRUTS2 configuration file:
<action name= "Down_zip" class= "com.hli.action.report.ReportAction" > <result name= "Success"
Stream ">
<param name=" ContentType ">application/zip</param>
<param name=" InputName "> downzipstream</param>
<param name= "contentdisposition" >attachment;filename= "${downFileName}" </param>
<param name= "buffersize" >4096</param>
</result>
Once the above programming is completed, the required requirements are implemented without temporary file generation.
Note: Upload Java method implementation needs optimization, write down for the time being, to be perfected later