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/

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM