struts2下载文件功能(边下载边打包)

多个文件,目录不同,通过条件查询如何进行打包下载呢?

1.利用ZipEntry进i行文件的压缩

2.前台jsp传入需要打包下载的一系列的文件的路径(数组类型)。因为是在checkBox中,表单提交会自动将其定义成数组。只需要将name名称命名成后台需要得到的路径数组名称

比如前台

downLoadZip.jsp

--------checkBox处代码-------------------------------

利用iterator迭代出来的filePath

<input type="checkbox" name="downLoadPaths"
value='<s:property value="filePath"/>'/>



后台Action

private String[] downLoadPaths;

对downLoadPaths进行遍历,打包。。。。

代码:

Java代码 
  1. /** 
  2.  * 批量下载(压缩成zip,然后下载)。不创建临时文件 
  3.  *  
  4.  * @author Cloudy 
  5.  *  
  6.  */  
  7. @Namespace("xxxxxx")  
  8. public class DownZipAction {  
  9.     /** 
  10.      *  
  11.      */  
  12.     private static final long serialVersionUID = 1L;  
  13.     // 传递一个List<String>()对象传值路径合集  
  14.     private String[] downLoadPaths;  
  15.     private OutputStream res;  
  16.     private ZipOutputStream zos;  
  17.     private String outPath;  
  18.   
  19.     // Action主方法  
  20.     @Action(value="DownLoadZip",results={@Result(name="nodata",location="/error.jsp"),  
  21.             @Result(name="success",location="xxxx.jsp")})  
  22.     public String downLoadZip() throws Exception {  
  23.         // 有数据可以下载  
  24.         if (downLoadPaths.length != 0) {  
  25.             // 进行预处理  
  26.             preProcess();  
  27.         } else {  
  28.             // 没有文件可以下载,返回nodata  
  29.             return "nodata";  
  30.         }  
  31.         // 处理  
  32.         writeZip(downLoadPaths);  
  33.         // 后处理关闭流  
  34.         afterProcess();  
  35.         return SUCCESS;  
  36.     }  
  37.   
  38.     // 压缩处理  
  39.     public void writeZip(String[] downLoadPaths) throws IOException {  
  40.         byte[] buf = new byte[8192];  
  41.         int len;  
  42.           
  43.         for (String filename : downLoadPaths) {  
  44.             File file = new File(filename);  
  45.             if (!file.isFile())  
  46.                 continue;  
  47.             ZipEntry ze = new ZipEntry(file.getName()); //apache jar的ZipEntry  
  48.             zos.putNextEntry(ze);  
  49.             BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));  
  50.             while ((len = bis.read(buf)) > 0) {  
  51.                 zos.write(buf, 0, len);  
  52.             }  
  53.             bis.close();  
  54.             zos.closeEntry();  
  55.         }  
  56.     }  
  57.   
  58.     // 预处理  
  59.     public void preProcess() throws Exception {  
  60.         HttpServletResponse response = ServletActionContext.getResponse();  
  61.         res = response.getOutputStream();  
  62.         // 清空输出流(在迅雷下载不会出现一长窜)  
  63.         response.reset();  
  64.         // 设定输出文件头  
  65.         response.setHeader("Content-Disposition""attachment;filename=document.zip");  
  66.         response.setContentType("application/zip");  
  67.         zos = new ZipOutputStream(res);  
  68.     }  
  69.   
  70.     // 后处理  
  71.     public void afterProcess() throws IOException {  
  72.   
  73.         zos.close();  
  74.         res.close();  
  75.     }  
  76.   
  77.     public OutputStream getRes() {  
  78.         return res;  
  79.     }  
  80.   
  81.     public void setRes(OutputStream res) {  
  82.         this.res = res;  
  83.     }  
  84.   
  85.     public ZipOutputStream getZos() {  
  86.         return zos;  
  87.     }  
  88.   
  89.     public void setZos(ZipOutputStream zos) {  
  90.         this.zos = zos;  
  91.     }  
  92.   
  93.     public String[] getDownLoadPaths() {  
  94.         return downLoadPaths;  
  95.     }  
  96.   
  97.     public void setDownLoadPaths(String[] downLoadPaths) {  
  98.         this.downLoadPaths = downLoadPaths;  
  99.     }  
  100.   
  101.     public String getOutPath() {  
  102.         return outPath;  
  103.     }  
  104.   
  105.     public void setOutPath(String outPath) {  
  106.         this.outPath = outPath;  
  107.     }  
  108.   
  109. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值