Java批量文件打包下载

时间:2023-03-09 19:34:39
Java批量文件打包下载

经常遇到选择多个文件进行批量下载的情况,可以先将选择的所有的文件生成一个zip文件,然后再下载,该zip文件,即可实现批量下载,但是在打包过程中,常常也会出现下载过来的zip文件中里面有乱码的文件名,通过使用ant.jar中的org.apache.tools.zip里的ZipOutPutStream为实现编码的设置。 
代码如下:

ant包引用

  1. <span style="font-size:14px">Xml代码
  2. <dependency>
  3. <groupId>ant</groupId>
  4. <artifactId>ant</artifactId>
  5. <version>1.6.5</version>
  6. </dependency>  </span>

压缩下载的action代码

  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. * 批量下载文件:
  19. *   使用ant.jar包中的org.apache.tools.zip.*完成压缩,
  20. * java原生也有java.util.zip.*但是测试了下无法搞定压缩
  21. * 文件内文件名的中文问题
  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. //生成的ZIP文件名为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. // 需要同时下载的两个文件result.txt ,source.txt
  38. File[] file1 = { new File(FilePath+"test1.txt"),
  39. new File(FilePath+"测试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. //设置压缩文件内的字符编码,不然会变成乱码
  44. out.setEncoding("GBK");
  45. int len;
  46. // 读入需要下载的文件的内容,打包到zip文件
  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("文件下载出错", e);
  57. }
  58. return null;
  59. }
  60. /**
  61. * 获取Response
  62. * @return
  63. */
  64. private HttpServletResponse getResponse() {
  65. return ServletActionContext.getResponse();
  66. }
  67. /**
  68. * 文件下载
  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);// 放到缓冲流里面
  79. OutputStream outs = response.getOutputStream();// 获取文件输出IO流
  80. BufferedOutputStream bouts = new BufferedOutputStream(outs);
  81. response.setContentType("application/x-download");// 设置response内容的类型
  82. response.setHeader(
  83. "Content-disposition",
  84. "attachment;filename="
  85. + URLEncoder.encode(str, "UTF-8"));// 设置头部信息
  86. int bytesRead = 0;
  87. byte[] buffer = new byte[8192];
  88. // 开始向网络传输文件流
  89. while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
  90. bouts.write(buffer, 0, bytesRead);
  91. }
  92. bouts.flush();// 这里一定要调用flush()方法
  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("文件下载出错", e);
  102. }
  103. }
  104. }
  105. 经过在windows环境下测试通过,使用struts2</span>

http://blog.csdn.net/clare504/article/details/11962263/