Often encountered the choice of multiple files for bulk download, you can first select all the files to generate a zip file, and then download, the zip file, you can achieve bulk download, but in the packaging process, often also appear in the downloaded ZIP file contains garbled file name, The encoding is implemented by using the Zipoutputstream in the Org.apache.tools.zip in Ant.jar.
The code is as follows:
Ant Package Reference
[HTML]View PlainCopyprint?
- <span style="font-size:14px">xml code
- <dependency>
- <groupId>ant</groupId>
- <artifactid>ant</artifactid>
- <version>1.6.5</version>
- </Dependency> </span>
Compress the downloaded Action code
[HTML]View PlainCopyprint?
- <span style="font-size:14px">package demo.action;
- Import Java.io.BufferedInputStream;
- Import Java.io.BufferedOutputStream;
- Import Java.io.File;
- Import Java.io.FileInputStream;
- Import Java.io.FileOutputStream;
- Import java.io.IOException;
- Import Java.io.InputStream;
- Import Java.io.OutputStream;
- Import Java.net.URLEncoder;
- Import Javax.servlet.http.HttpServletResponse;
- Import Org.apache.log4j.Logger;
- Import Org.apache.struts2.ServletActionContext;
- Import Org.apache.tools.zip.ZipEntry;
- Import Org.apache.tools.zip.ZipOutputStream;
- Import Com.opensymphony.xwork2.ActionSupport;
- /**
- * Bulk Download files:
- * Use the org.apache.tools.zip.* in the Ant.jar package to complete the compression,
- * Java native also has java.util.zip.* but the test does not take care of the compression
- * Chinese problem with file name in document
- * @author Yangcong
- *
- */
- public class Batchdownloadaction extends Actionsupport {
- Private Logger Log = Logger.getlogger (batchdownloadaction.class);
- private static final String FilePath = "d:\\";
- Private static final long serialversionuid = -8694640030455344419l;
- Public String execute () {
- The generated zip file is named Demo.zip
- String tmpfilename = "Demo.zip";
- byte[] buffer = new byte[1024];
- String Strzippath = FilePath + tmpfilename;
- try {
- Zipoutputstream out = new Zipoutputstream (New FileOutputStream (
- Strzippath));
- Two files that need to be downloaded simultaneously result.txt, source.txt
- file[] file1 = {new File (filepath+ "Test1.txt"),
- New File (filepath+ "Test 2.docx")};
- for (int i = 0; I < file1.length; i++) {
- FileInputStream fis = new FileInputStream (File1[i]);
- Out.putnextentry (New ZipEntry (File1[i].getname ()));
- Set the character encoding within the compressed file, or it will become garbled
- Out.setencoding ("GBK");
- int Len;
- Read the contents of the file to be downloaded and package it into a zip file
- while (len = fis.read (buffer)) > 0) {
- Out.write (buffer, 0, Len);
- }
- Out.closeentry ();
- Fis.close ();
- }
- Out.close ();
- This.downfile (GetResponse (), tmpfilename);
- } catch (Exception e) {
- Log.error ("File download Error", E);
- }
- return null;
- }
- /**
- * Get response
- * @return
- */
- Private HttpServletResponse GetResponse () {
- return Servletactioncontext.getresponse ();
- }
- /**
- * File Download
- * @param response
- * @param str
- */
- private void Downfile (httpservletresponse response, String str) {
- try {
- String Path = FilePath + str;
- File file = new file (path);
- if (file.exists ()) {
- InputStream ins = new FileInputStream (path);
- Bufferedinputstream bins = new Bufferedinputstream (INS);//put in the buffer stream
- OutputStream outs = response.getoutputstream ();//Get file output IO stream
- Bufferedoutputstream bouts = new Bufferedoutputstream (outs);
- Response.setcontenttype ("Application/x-download");//Set the type of response content
- Response.setheader (
- "Content-disposition",
- "Attachment; filename= "
- + Urlencoder.encode (str, "UTF-8"));//Set Header information
- int bytesread = 0;
- byte[] buffer = new byte[8192];
- Start transferring file streams to the network
- while ((bytesread = bins.read (buffer, 0, 8192))! =-1) {
- Bouts.write (buffer, 0, bytesread);
- }
- Bouts.flush ();//The Flush () method must be called here
- Ins.close ();
- Bins.close ();
- Outs.close ();
- Bouts.close ();
- } else {
- Response.sendredirect (".. /error.jsp ");
- }
- } catch (IOException e) {
- Log.error ("File download Error", E);
- }
- }
- }
- After testing in a Windows environment, use STRUTS2</span>
[HTML]View PlainCopyprint?
[HTML]View PlainCopyprint?
- <span style="font-size:14px"> Resource Ant.jar:<a target="_blank" href= "http://blog.csdn.net/clare504/article/details/11962263">http://blog.csdn.net/clare504/article/ details/11962263</a></span>
http://blog.csdn.net/clare504/article/details/11962263/
Java Batch file package download