批量下载

 /**
 * 批量下载
 * @param request
 * @param response
 * @return void
 */
 @RequestMapping("/PolicyController!batchFileDownload")
 public void batchFileDownload(HttpServletRequest request,HttpServletResponse response){
  
//  1. 批量下载多个文件时,系统将所有文件打包成压缩文件,命名规则:政策类型(当前时间),时间精确到时分秒,如“月度商务政策(20130910111501)”
//  2. 下载功能受权限控制;
//  3. 单次下载文件不得超过200M,否则提示用户;
  
  //政策分类(1:月度政策查询; 2:年度政策查询;3:产品手册查询;4:售后政策查询;5:其他政策查询;)
  //String policyCategory =  request.getParameter("category");
  String policyCategory = request.getParameter("category");
  if(policyCategory == null || "".equals(policyCategory)){
   policyCategory = "1";
  }
//policyCategory = "2";  
  //文件名称(数字名称)
  String filePaths = request.getParameter("filePaths");
//filePaths = "201310141018309019.pdf,201310140940426989.pdf";  
  String[] filePathSplt = filePaths.split(",");
  
//  String fileName = request.getParameter("");
//fileName = "比亚迪-201310-B-关于开展(科技十年十重大礼)全线跨年盛惠营销活动的通知.pdf,花样百出城.xml";  
// String[] fileNameSplt = fileName.split(",");

  String businessPolicyPath = PolicyConstants.policyDownloadPath;
  
  //临时目录(文件夹)
  String tempPath = request.getSession().getServletContext().getRealPath("/")+"policy_temp\\";
  
  File[] downloadFiles = new File[filePathSplt.length];
  
  for(int i=0;i<filePathSplt.length;i++){
   
   String chineseName = policyService.updateFileNameAndgetFileChineseNameByNumName(tempPath, businessPolicyPath, filePathSplt[i]);
   
   if(!"".equals(chineseName)){
    downloadFiles[i] = new File(chineseName);
   }
   
   /***********start*********增加文件下载数据(用于判断此文件下载状态)*******************/
   
   //当前sessionID
   String sessionId = request.getSession().getId();
   
   Date nowTime = new Date();
   
   String path = PolicyConstants.policyDownloadPath + filePathSplt[i];
   
   File file = new File(path);
   
   //文件大小(单位:字节(B))  B-->M,B/1024/1024 = M
   long fileSize = file.length();
   
   DownloadFileInfo info = new DownloadFileInfo();
   info.setFileName(chineseName.substring(chineseName.lastIndexOf("\\")+1));
   info.setFilePath(filePathSplt[i]);
   info.setSessionId(sessionId);
   info.setInsertTime(nowTime);
   info.setFileSize(fileSize);
   
   policyService.insertDownloadFileData(info);
   
   /***********end*********增加文件下载数据(用于判断此文件下载状态)*******************/
  }
  
  DownloadUtil util = new DownloadUtil();
  
  //文件名称(精确到毫秒)
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  
  String zipName = "";
  if("1".equals(policyCategory)){
   zipName = PolicyConstants.monthPolicyName;
  }else if("2".equals(policyCategory)){
   zipName = PolicyConstants.yearPolicyName;
  }else if("3".equals(policyCategory)){
   zipName = PolicyConstants.productManualName;
  }else if("4".equals(policyCategory)){
   zipName = PolicyConstants.afterSaleyPolicyName;
  }else if("5".equals(policyCategory)){
   zipName = PolicyConstants.otherPolicyName;
  }
  
  //.zip文件名称
  String zipFileName = zipName + "(" + sdf.format(new Date()) + ")"; 
  
  //zip(无数据)文件绝对目录
  String path = request.getSession().getServletContext().getRealPath("/")+"policy_temp\\"+zipFileName+".zip";
  
  //zip(有数据)文件绝对目录
  String zipPath = "";
  try {
   zipPath = util.getZipPath(downloadFiles, path);
  } catch (Exception e1) {
   e1.printStackTrace();
  }
  
  File filepatch = new File(zipPath);
  
  //文件大小(单位:字节(B))  B-->M,B/1024/1024 = M
  long fileSize = filepatch.length();
  String overLimitSizeTip = "";
  
  InputStream is = null;
  OutputStream fileOut = null;
  
  if (filepatch.exists()) {
   try {
    is = new FileInputStream(zipPath); 
    response.setContentType("application/zip;charset=UFT-8");
    response.setHeader("Content-Disposition","attachment; filename=" + PolicyUtils.toUtf8String(request,zipFileName+".zip"));
    fileOut = response.getOutputStream();
    byte[] dd = new byte[1024];
    while(is.read(dd, 0, dd.length) != -1){
     fileOut.write(dd);
    }
    fileOut.flush();
    
   } catch (Exception e) {
    e.printStackTrace();
   }finally{
    if(fileOut!=null){
     try {
      fileOut.close();
     } catch (Exception e) {}
    }
    if(is!=null){
     try {
      is.close();
     } catch (Exception e) {}
    }
    //删除每个临时文件
    for(int jj=0;jj<downloadFiles.length;jj++){
     if (downloadFiles[jj].exists()) {
      downloadFiles[jj].delete();
     }
    }
    //删除zip文件
    if (filepatch.exists()) {
     filepatch.delete();
    }
   }
  }
 }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值