网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

小编给大家分享一下Java实现批量下载选中文件功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:国际域名空间、雅安服务器托管、营销软件、网站建设、临海网站维护、网站推广。

1.在action中定义变量

 private List downLoadPaths = new ArrayList();//存储选中文件的下载地址 
 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 fileVos = new LinkedList(); 
    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 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中配置

//class值为bean.xml中配置的bean 
   
    204//表示响应执行成功,但没有数据返回,浏览器不用刷新,不用导向新页面 
   

  用到的jar包

以上是“Java实现批量下载选中文件功能”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


文章标题:Java实现批量下载选中文件功能
浏览路径:http://bjjierui.cn/article/goijgc.html

其他资讯