Java实现批量下载选中文件功能

作者:秋分中的雨 时间:2021-09-09 17:57:45 

1.在action中定义变量


private List<String> downLoadPaths = new ArrayList<String>();//存储选中文件的下载地址
private OutputStream res;
private ZipOutputStream zos;
private String outPath;
private String lessionIdStr;// 选中文件ID拼接的字符串
private String fileName; //浏览器下载弹出框中显示的文件名

  分别给出get和set方法

2.  主方法 


/**
  * 下载多个文件:压缩成zip
  *
  * @return
  * @throws Exception
  */
 public String downLoadLessionsZip() {
   downLoadPaths.clear();
   String firstFileName = "";// 第一个文件的文件名
   List<DownLoadFileVo> fileVos = new LinkedList<DownLoadFileVo>();
   if (StringUtils.isNotEmpty(lessionIdStr)) {
     int end = lessionIdStr.lastIndexOf(",");
     if (end > 0) {
       if (end == lessionIdStr.length() - 1) {
         lessionIdStr = lessionIdStr.substring(0, end);
       }
       String[] ids = lessionIdStr.split(",");
       for (int i = 0; i < ids.length; i++) {
         if (StringUtils.isNumeric(ids[i])) {
           BkPersonLession lession = bkPersonLessionService.downLoadLession(Integer.parseInt(ids[i]));
           if (lession != null) {
             fileVos.add(new DownLoadFileVo(lession
                 .getLessionName(), getContextRealPath()
                 + lession.getLessionSavePath()));
             downLoadPaths.add(getContextRealPath()
                 + lession.getLessionSavePath());
           }
           if (i == 0) {              
                      firstFileName = lession.getLessionName();
           }
         }
       }
     }
   }
   // 有数据可以下载
   if (downLoadPaths.size() != 0) {
     // 进行预处理
     preProcess(firstFileName);
   } else {
     // 没有文件可以下载,返回nodata
     return "nodata";
   }
   // 处理
   writeZip(fileVos);
   // 后处理关闭流
   afterProcess();
   return null;
 }
 // 压缩处理
 public void writeZip(List<DownLoadFileVo> fileVos) {
   byte[] buf = new byte[8192];
   int len;
   for (DownLoadFileVo fileVo : fileVos) {
     File file = new File(fileVo.getFileSavePath());
     if (!file.isFile())
       continue;
     ZipEntry ze = new ZipEntry(fileVo.getFileName()
         + fileVo.getFileSavePath().substring(
             fileVo.getFileSavePath().lastIndexOf(".")));                          
     try {
       zos.putNextEntry(ze);
       BufferedInputStream bis = new BufferedInputStream(
           new FileInputStream(file));
       while ((len = bis.read(buf)) > 0) {
         zos.write(buf, 0, len);
       }
       bis.close();
       zos.closeEntry();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
 // 预处理
 public void preProcess(String firseFileName) {
   String zipName = "【批量下载】" + firseFileName + "等.zip";
   String filename = "";
   try {
     filename = new String(zipName.getBytes("GBK"), "8859_1");
   } catch (UnsupportedEncodingException e1) {
     e1.printStackTrace();
   }
   this.fileName = filename;
   HttpServletResponse response = ServletActionContext.getResponse();
   try {
     res = response.getOutputStream();
     // 清空输出流(在迅雷下载不会出现一长窜)
     response.reset();
     // 设定输出文件头
     response.setHeader("Content-Disposition", "attachment;fileName="
         + filename);
     response.setContentType("application/zip");
     zos = new ZipOutputStream(res);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
 // 后处理
 public void afterProcess() {
   try {
     if (zos != null) {
       zos.close();
     }
     if (res != null) {
       res.close();
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }

3. 在struts.xml中配置


<action name="downLoadBkPersonLessionsZip" class="bkPersonLessionAction"  
     method="downLoadLessionsZip">//class值为bean.xml中配置的bean
 <result name="nodata" type="httpheader">
   <param name="status">204</param>//表示响应执行成功,但没有数据返回,浏览器不用刷新,不用导向新页面
 </result>
</action>

  用到的jar包

总结

以上所述是小编给大家介绍的Java实现批量下载选中文件功能网站的支持!

来源:http://blog.csdn.net/kosum/article/details/47394321

标签:java,下载文件
0
投稿

猜你喜欢

  • 关于Mybatis与JPA的优缺点说明

    2023-08-23 22:28:33
  • C#实现字体旋转的方法

    2023-01-19 06:41:40
  • Java多线程死锁与资源限制操作

    2023-08-31 05:54:30
  • mybatis使用Integer类型查询可能出现的问题

    2022-09-01 12:47:38
  • java开发ShardingSphere的路由引擎类型示例详解

    2023-11-29 01:18:56
  • C#中跨线程访问控件问题解决方案分享

    2021-06-27 18:47:24
  • Java分布式服务框架Dubbo介绍

    2022-09-16 01:27:53
  • 深入浅析c#静态多态性与动态多态性

    2022-10-16 09:41:17
  • Java swing五子棋的实现方法

    2021-06-01 15:25:20
  • 详解Java中类的加载与其初始化

    2023-06-21 04:56:45
  • 关于Mybatis-Plus Wrapper是否应该出现在Servcie类中

    2023-11-28 22:04:56
  • hashCode方法的使用讲解

    2022-11-12 15:29:37
  • SpringCloud网关Gateway架构解析

    2023-03-17 00:18:42
  • spring 注解如何开启声明式事务

    2023-04-09 23:07:42
  • C++实现LeetCode(2.两个数字相加)

    2023-06-23 16:51:11
  • Spring主配置文件(applicationContext.xml) 导入约束详解

    2021-09-29 06:21:01
  • java poi导入纯数字等格式问题及解决

    2023-04-14 08:50:30
  • 浅谈SpringMVC+Spring3+Hibernate4开发环境搭建

    2023-08-06 13:49:19
  • 带你了解Java数据结构和算法之数组

    2022-09-29 21:18:33
  • mybatis 映射文件中if标签判断字符串相等的两种方式

    2023-08-02 16:06:44
  • asp之家 软件编程 m.aspxhome.com