Java 批量下载 压缩文件 rar格式

实体类

public class app
{
    public String FileName;

    public String getFileName() {
        return FileName;
    }

    public void setFileName(String fileName) {
        FileName = fileName;
    }
    public File myFile;

    public File getMyFile() {
        return myFile;
    }

    public void setMyFile(File myFile) {
        this.myFile = myFile;
    }

    public app(){
    }
}
后台代码

/*批量下载*/
public void getRARFilearry() throws IOException {
   String idd = Struts2Utils.getParameter("id");
   String filename = Struts2Utils.getParameter("fileOnlyName");
   String fileid []= idd.split(",");       
   String filenameList []= filename.split(",");
   System.out.println(fileid.length);
   app[] srcfiles = new app[fileid.length];
   File zipfile = null;
   String adress = null ;
   String suff = ".rar";
   if(fileid.length == filenameList.length){
      for (int i=0;i<fileid.length;i++){
         String fileNameid =  fileid[i];
         String fileNameMZ =  filenameList[i];
         List<Record> list = documentManager.queryRecordById(fileNameid);
         adress = list.get(0).getUnit();
         String tzname = list.get(0).getStationname();
         String Name = adress.concat(tzname);
         String fileName = Name.concat(suff);
         System.out.println(fileName);
         filePath = ServletActionContext.getServletContext().getRealPath("/") + "files";
         File file = new File(filePath + "/" + fileNameMZ+".rar");
         app ap=new app();
         ap.setFileName(fileName);
         ap.setMyFile(file);
         srcfiles[i]=ap;
         //srcfile[i]=file;
         fileName = FileUtils.encodeDownloadFileName(fileName, Struts2Utils.getRequest().getHeader("USER-AGENT").toString());
         zipfile = new File(filePath+ "/" +fileName);

      }
   }
   DocumentAction.zipFiles(srcfiles, zipfile);    // 方法名 DocumentAction
   String name = adress.concat(suff);
   FileUtils.download(name, zipfile, "application/octet-stream", false, Struts2Utils.getResponse());

}


/**
 * 功能:压缩多个文件成一个zip文件
 * @param srcfile:源文件列表
 * @param zipfile:压缩后的文件
 */
public static void zipFiles(app[] srcfile, File zipfile) throws IOException {
   byte[] buf = new byte[1024];
   try {
      //ZipOutputStream类:完成文件或文件夹的压缩
      ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
      for (int i = 0; i < srcfile.length; i++) {
         FileInputStream in = new FileInputStream(srcfile[i].myFile);
         out.putNextEntry(new ZipEntry(srcfile[i].getFileName()));
         int len;
         while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
         }
         out.closeEntry();
         in.close();
      }
      out.close();
      System.out.println("压缩完成.");
   } catch (Exception e) {
      String name = "下载失败";
      FileUtils.download(name, zipfile, "application/octet-stream", false, Struts2Utils.getResponse());
      throw new IOException("下载失败!");
   }
}


猜你喜欢

转载自blog.csdn.net/qq_38029917/article/details/79157740