欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Java實(shí)現(xiàn)批量下載選中文件功能

小編給大家分享一下Java實(shí)現(xiàn)批量下載選中文件功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:國際域名空間、雅安服務(wù)器托管、營銷軟件、網(wǎng)站建設(shè)、臨海網(wǎng)站維護(hù)、網(wǎng)站推廣。

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.  主方法 

/** 
   * 下載多個(gè)文件:壓縮成zip 
   * 
   * @return 
   * @throws Exception 
   */ 
  public String downLoadLessionsZip() { 
    downLoadPaths.clear(); 
    String firstFileName = "";// 第一個(gè)文件的文件名 
    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(); 
            } 
          } 
        } 
      } 
    } 
    // 有數(shù)據(jù)可以下載 
    if (downLoadPaths.size() != 0) { 
      // 進(jìn)行預(yù)處理 
      preProcess(firstFileName); 
    } else { 
      // 沒有文件可以下載,返回nodata 
      return "nodata"; 
    } 
    // 處理 
    writeZip(fileVos); 
    // 后處理關(guān)閉流 
    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(); 
      } 
    } 
  } 
  // 預(yù)處理 
  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(); 
      // 清空輸出流(在迅雷下載不會出現(xiàn)一長竄) 
      response.reset(); 
      // 設(shè)定輸出文件頭 
      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>//表示響應(yīng)執(zhí)行成功,但沒有數(shù)據(jù)返回,瀏覽器不用刷新,不用導(dǎo)向新頁面 
  </result> 
</action>

  用到的jar包

以上是“Java實(shí)現(xiàn)批量下載選中文件功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前文章:Java實(shí)現(xiàn)批量下載選中文件功能
當(dāng)前網(wǎng)址:http://chinadenli.net/article12/goijgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站移動網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈網(wǎng)站設(shè)計(jì)做網(wǎng)站定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)