Java realizes the function of batch downloading selected files

  • 2020-11-18 06:11:26
  • OfStack

1. Define variables in action


 private List<String> downLoadPaths = new ArrayList<String>();// Store the download address of the selected file  
 private OutputStream res; 
 private ZipOutputStream zos; 
 private String outPath; 
 private String lessionIdStr;//  Select the file ID Concatenated string  
 private String fileName; // The browser downloads the file name that appears in the pop-up box  

The get and set methods are given respectively

2. The main method


/** 
   *  Download multiple files: compress to zip 
   * 
   * @return 
   * @throws Exception 
   */ 
  public String downLoadLessionsZip() { 
    downLoadPaths.clear(); 
    String firstFileName = "";//  The first 1 The filename of a file  
    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(); 
            } 
          } 
        } 
      } 
    } 
    //  The data is available for download  
    if (downLoadPaths.size() != 0) { 
      //  Pretreat  
      preProcess(firstFileName); 
    } else { 
      //  No files to download, return nodata 
      return "nodata"; 
    } 
    //  To deal with  
    writeZip(fileVos); 
    //  Post-processing closes the stream  
    afterProcess(); 
    return null; 
  } 
  //  The compression process  
  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(); 
      } 
    } 
  } 
  //  pretreatment  
  public void preProcess(String firseFileName) { 
    String zipName = " [Batch Download] " + firseFileName + " Etc. .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(); 
      //  Empty the output stream ( In Thunderbolt download will not appear 1 Long shot ) 
      response.reset(); 
      //  Set the output file header  
      response.setHeader("Content-Disposition", "attachment;fileName=" 
          + filename); 
      response.setContentType("application/zip"); 
      zos = new ZipOutputStream(res); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
  } 
  //  post-processing  
  public void afterProcess() { 
    try { 
      if (zos != null) { 
        zos.close(); 
      } 
      if (res != null) { 
        res.close(); 
      } 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
  } 

3. Configuration in ES12en.xml


<action name="downLoadBkPersonLessionsZip" class="bkPersonLessionAction"  
      method="downLoadLessionsZip">//class A value of bean.xml Configured in bean 
  <result name="nodata" type="httpheader"> 
    <param name="status">204</param>// Indicates that the response executed successfully, but no data is returned, the browser does not have to refresh, and it does not have to lead to a new page  
  </result> 
</action> 

The jar package used

conclusion


Related articles: