Java Batch file package download

Source: Internet
Author: User

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?
  1. <span style="font-size:14px">xml code
  2. <dependency>
  3. <groupId>ant</groupId>
  4. <artifactid>ant</artifactid>
  5. <version>1.6.5</version>
  6. </Dependency> </span>


Compress the downloaded Action code

[HTML]View PlainCopyprint?
  1. <span style="font-size:14px">package demo.action;
  2. Import Java.io.BufferedInputStream;
  3. Import Java.io.BufferedOutputStream;
  4. Import Java.io.File;
  5. Import Java.io.FileInputStream;
  6. Import Java.io.FileOutputStream;
  7. Import java.io.IOException;
  8. Import Java.io.InputStream;
  9. Import Java.io.OutputStream;
  10. Import Java.net.URLEncoder;
  11. Import Javax.servlet.http.HttpServletResponse;
  12. Import Org.apache.log4j.Logger;
  13. Import Org.apache.struts2.ServletActionContext;
  14. Import Org.apache.tools.zip.ZipEntry;
  15. Import Org.apache.tools.zip.ZipOutputStream;
  16. Import Com.opensymphony.xwork2.ActionSupport;
  17. /**
  18. * Bulk Download files:
  19. * Use the org.apache.tools.zip.* in the Ant.jar package to complete the compression,
  20. * Java native also has java.util.zip.* but the test does not take care of the compression
  21. * Chinese problem with file name in document
  22. * @author Yangcong
  23. *
  24. */
  25. public class Batchdownloadaction extends Actionsupport {
  26. Private Logger Log = Logger.getlogger (batchdownloadaction.class);
  27. private static final String FilePath = "d:\\";
  28. Private static final long serialversionuid = -8694640030455344419l;
  29. Public String execute () {
  30. The generated zip file is named Demo.zip
  31. String tmpfilename = "Demo.zip";
  32. byte[] buffer = new byte[1024];
  33. String Strzippath = FilePath + tmpfilename;
  34. try {
  35. Zipoutputstream out = new Zipoutputstream (New FileOutputStream (
  36. Strzippath));
  37. Two files that need to be downloaded simultaneously result.txt, source.txt
  38. file[] file1 = {new File (filepath+ "Test1.txt"),
  39. New File (filepath+ "Test 2.docx")};
  40. for (int i = 0; I < file1.length; i++) {
  41. FileInputStream fis = new FileInputStream (File1[i]);
  42. Out.putnextentry (New ZipEntry (File1[i].getname ()));
  43. Set the character encoding within the compressed file, or it will become garbled
  44. Out.setencoding ("GBK");
  45. int Len;
  46. Read the contents of the file to be downloaded and package it into a zip file
  47. while (len = fis.read (buffer)) > 0) {
  48. Out.write (buffer, 0, Len);
  49. }
  50. Out.closeentry ();
  51. Fis.close ();
  52. }
  53. Out.close ();
  54. This.downfile (GetResponse (), tmpfilename);
  55. } catch (Exception e) {
  56. Log.error ("File download Error", E);
  57. }
  58. return null;
  59. }
  60. /**
  61. * Get response
  62. * @return
  63. */
  64. Private HttpServletResponse GetResponse () {
  65. return Servletactioncontext.getresponse ();
  66. }
  67. /**
  68. * File Download
  69. * @param response
  70. * @param str
  71. */
  72. private void Downfile (httpservletresponse response, String str) {
  73. try {
  74. String Path = FilePath + str;
  75. File file = new file (path);
  76. if (file.exists ()) {
  77. InputStream ins = new FileInputStream (path);
  78. Bufferedinputstream bins = new Bufferedinputstream (INS);//put in the buffer stream
  79. OutputStream outs = response.getoutputstream ();//Get file output IO stream
  80. Bufferedoutputstream bouts = new Bufferedoutputstream (outs);
  81. Response.setcontenttype ("Application/x-download");//Set the type of response content
  82. Response.setheader (
  83. "Content-disposition",
  84. "Attachment; filename= "
  85. + Urlencoder.encode (str, "UTF-8"));//Set Header information
  86. int bytesread = 0;
  87. byte[] buffer = new byte[8192];
  88. Start transferring file streams to the network
  89. while ((bytesread = bins.read (buffer, 0, 8192))! =-1) {
  90. Bouts.write (buffer, 0, bytesread);
  91. }
  92. Bouts.flush ();//The Flush () method must be called here
  93. Ins.close ();
  94. Bins.close ();
  95. Outs.close ();
  96. Bouts.close ();
  97. } else {
  98. Response.sendredirect (".. /error.jsp ");
  99. }
  100. } catch (IOException e) {
  101. Log.error ("File download Error", E);
  102. }
  103. }
  104. }
  105. After testing in a Windows environment, use STRUTS2</span>
[HTML]View PlainCopyprint? [HTML]View PlainCopyprint?
    1. <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

Related Article

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.