zoukankan      html  css  js  c++  java
  • java+文件批量下载

    这篇文章主要介绍了Java实现批量下载选中文件功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    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&lt;DownLoadFileVo&gt; fileVos = new LinkedList&lt;DownLoadFileVo&gt;(); 
        if (StringUtils.isNotEmpty(lessionIdStr)) { 
          int end = lessionIdStr.lastIndexOf(","); 
          if (end &gt; 0) { 
            if (end == lessionIdStr.length() - 1) { 
              lessionIdStr = lessionIdStr.substring(0, end); 
            } 
            String[] ids = lessionIdStr.split(","); 
            for (int i = 0; i &lt; 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&lt;DownLoadFileVo&gt; 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)) &gt; 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> 

    总结

    以上所述是小编给大家介绍的Java实现批量下载选中文件功能,希望对大家有所帮助,如果大家有任何疑问请给我留言

     详细的代码及配置信息可以参考我写的这篇文章:

    http://blog.ncmem.com/wordpress/2019/08/28/java%e6%89%b9%e9%87%8f%e4%b8%8b%e8%bd%bd/

  • 相关阅读:
    最新java学习路线:含阶段性java视频教程完整版
    2019最新WEB前端开发小白必看的学习路线(附学习视频教程)
    区块链技术学习路线(全网最新)
    java学习路线之必会的java基础教程
    新手如何学习python(python学习路线图)
    python学习教程,史上最全面的python学习路线图
    机器学习中的误差 Where does error come from?
    主成分分析 Principle Component Analysis
    线性回归 Linear Regression
    MCtalk对话尚德机构:AI讲师,假套路还是真功夫?
  • 原文地址:https://www.cnblogs.com/songsu/p/11307398.html
Copyright © 2011-2022 走看看