分享

spring mvc 批量下载压缩文件夹中文件

 青_春 2017-07-11

一、工具类

[java] view plain copy
  1. package com.juanpi.service.util;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.OutputStream;  
  10. import java.util.zip.ZipEntry;  
  11. import java.util.zip.ZipOutputStream;  
  12.   
  13. import javax.servlet.http.HttpServletResponse;  
  14.   
  15. public class CustomFileUtil {  
  16.     /** 
  17.      *  
  18.      * @param inputFileName 
  19.      *            输入一个文件夹 
  20.      * @param zipFileName 
  21.      *            输出一个压缩文件夹,打包后文件名字 
  22.      * @throws Exception 
  23.      */  
  24.     public static void zip(String inputFileName, String zipFileName) throws Exception {  
  25.         zip(zipFileName, new File(inputFileName));  
  26.     }  
  27.   
  28.     private static void zip(String zipFileName, File inputFile) throws Exception {  
  29.         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));  
  30.         zip(out, inputFile, "");  
  31.         out.close();  
  32.     }  
  33.   
  34.     private static void zip(ZipOutputStream out, File f, String base) throws Exception {  
  35.         if (f.isDirectory()) { // 判断是否为目录  
  36.             File[] fl = f.listFiles();  
  37.             out.putNextEntry(new ZipEntry(base + "/"));  
  38.             base = base.length() == 0 ? "" : base + "/";  
  39.             for (int i = 0; i < fl.length; i++) {  
  40.                 zip(out, fl[i], base + fl[i].getName());  
  41.             }  
  42.         } else { // 压缩目录中的所有文件  
  43.             out.putNextEntry(new ZipEntry(base));  
  44.             FileInputStream in = new FileInputStream(f);  
  45.             int b;  
  46.             while ((b = in.read()) != -1) {  
  47.                 out.write(b);  
  48.             }  
  49.             in.close();  
  50.         }  
  51.     }  
  52.   
  53.     /** 
  54.      * 下载文件 
  55.      *  
  56.      * @param file 
  57.      * @param response 
  58.      */  
  59.     public static void downloadFile(File file, HttpServletResponse response, boolean isDelete) {  
  60.         try {  
  61.             // 以流的形式下载文件。  
  62.             BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));  
  63.             byte[] buffer = new byte[fis.available()];  
  64.             fis.read(buffer);  
  65.             fis.close();  
  66.             // 清空response  
  67.             response.reset();  
  68.             OutputStream toClient = new BufferedOutputStream(response.getOutputStream());  
  69.             response.setContentType("application/octet-stream");  
  70.             response.setHeader("Content-Disposition",  
  71.                     "attachment;filename=" + new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"));  
  72.             toClient.write(buffer);  
  73.             toClient.flush();  
  74.             toClient.close();  
  75.             if (isDelete) {  
  76.                 file.delete(); // 是否将生成的服务器端文件删除  
  77.             }  
  78.         } catch (IOException ex) {  
  79.             ex.printStackTrace();  
  80.         }  
  81.     }  
  82.   
  83. }  

二、restful接口

[java] view plain copy
  1. /** 
  2.  * 批量打包下载文件生成zip文件下载 
  3.  *  
  4.  * @param session 
  5.  */  
  6. @RequestMapping(value = "/loadFiles", method = RequestMethod.GET)  
  7. public void downloadFiles(HttpServletRequest request, HttpServletResponse response,String param)  
  8.         throws ServletException, IOException {  
  9.     //String inputFileName = realPath; 可配置文件中配置磁盘路径,路径传参数拼接,得到文件夹下的子文件夹  
  10.     String inputFileName = "C:/test/";  
  11.     if (domain != null && !domain.trim().equals("")) {  
  12.         inputFileName = realPath + domain.toUpperCase();  
  13.     }  
  14.     logger.info("download file directory is :" + inputFileName);  
  15.     String zipFileName = realPath + "temp.zip"// 压缩后的zip文件 可随意定一个磁盘路径或者相对路径  
  16.     try {  
  17.         CustomFileUtil.zip(inputFileName, zipFileName);  
  18.     } catch (Exception ex) {  
  19.         logger.error(ex.getMessage());  
  20.     }  
  21.     File temp = new File(zipFileName);  
  22.     CustomFileUtil.downloadFile(temp, response, true);  
  23. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多