java 打包下载文件

1 篇文章 0 订阅

String path = null ;
  
  InputStream in = null;
  String linShiPath = FrameworkContext.getAppPath()+"\\"+"zip"; //打包生成zip
  File linShiFile = new File(linShiPath);
  
  if( !linShiFile.exists()){
   
   linShiFile.mkdir();
  }
 
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  
  String zipName = format.format(new Date()); //包名
 
  File outZipFile = new File(linShiPath+"\\"+zipName+".zip"); //生成的zip文件
  try {
   
   ZipOutputStream zipOut = new ZipOutputStream(outZipFile);
   
   InputStream input ; //将文件写出web
   
   for(int i = 0 ; i < arrIds.length ; i++){ //循环文件id得到所有的文件
    
    id = arrIds[i];
    //得到该id下的所有文件
    List<UploadFile> fileList = uploadFile.listFilesByPid("ES_T_RULEFILES", id); //下载的文件信息
    
    for(int j=0;j<fileList.size();j++){ //循环文件list 得到单个文件累加到zip中
     UploadFile upload = fileList.get(j) ;
     path = FrameworkContext.getAppPath()+upload.getPath();

     File downFile = new File(path); //要下载的文件
     
     in = new FileInputStream(downFile);
     
     zipOut.putNextEntry(new ZipEntry(upload.getName()));
     
     //写入zip文件
     int len = 0;
     
     byte b [] = new byte[1024] ;
     
     while((len = in.read(b)) > -1){
      
      zipOut.write(b , 0, len);
     }
     
     in.close();
     
     zipOut.closeEntry();
    }
    
   }
   
   zipOut.close();
   
   //将文件写出
   
   if(outZipFile.exists()){
    
    input = new FileInputStream(outZipFile) ;
    
    String exp = outZipFile.getName().substring(outZipFile.getName().lastIndexOf("."));
    
    try {
     
     postFile(request, response, input, outZipFile.getName(), exp, "UTF-8");
    
    } catch (Exception e) {
     
     PrintWriter out = null;
     response.setContentType("text/html;charset="
       + System.getProperties().get("file.encoding"));
     out = new PrintWriter(response.getOutputStream());
     out.write("服务器上不存在该文件,可能已被删除!");
     out.close();
     
     e.printStackTrace();
    
    }finally{
     
     input.close();
    }
    
   }else{
    
    PrintWriter out = null;
    response.setContentType("text/html;charset="
      + System.getProperties().get("file.encoding"));
    out = new PrintWriter(response.getOutputStream());
    out.write("服务器上不存在该文件,可能已被删除!");
    out.close();
    
   }
   
  } catch (Exception e) {
   
   PrintWriter out = null;
   response.setContentType("text/html;charset="
     + System.getProperties().get("file.encoding"));
   out = new PrintWriter(response.getOutputStream());
   out.write("服务器上不存在该文件,可能已被删除!");
   out.close();
   
   e.printStackTrace();
  
  }finally{
   
   outZipFile.delete();
   
   linShiFile.delete();
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值