将文件包压缩下载

private String filePath = "D:\\message\\";   
private String filePathZip = "D:\\messageZip\\Message.zip";  
  

 

   /**  
    * 文件下载  
    * @param response  
    * @param filePathZip  
    */  
private void downFile(HttpServletResponse response, String filePathZip) {   
    InputStream ins = null;
    BufferedInputStream bins = null;
    BufferedOutputStream bouts = null;
    OutputStream outs = null;
       try {   
           String path = filePathZip;   
           File file = new File(path);   
           if (file.exists()) {   
        //时间戳
        String formatDate = DateHelper.formatDate(DateHelper.getCurrentDate(), "yyyyMMdd");
               ins = new FileInputStream(path);   
               bins = new BufferedInputStream(ins);// 放到缓冲流里面   
               outs = response.getOutputStream();// 获取文件输出IO流   
               bouts = new BufferedOutputStream(outs);   
               response.setContentType("application/x-download");// 设置response内容的类型   
               response.setHeader(   "Content-disposition","attachment;filename="+ filePathZip.substring(14,21)+formatDate+filePathZip.substring(21));// 设置头部信息   
               int bytesRead = 0;   
               byte[] buffer = new byte[8192];   
               // 开始向网络传输文件流   
               while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {   
                   bouts.write(buffer, 0, bytesRead);   
               }   
               bouts.flush();// 这里一定要调用flush()方法   
               outs.close();   
              
           } else {   
               throw new RuntimeException("要下载的文件生成错误");
           }   
       } catch (IOException e) {   
        LogHelper.error(ACTION_NAME, e.toString());
       } 
       finally{
        if(outs!=null){
        try {
outs.close();
} catch (IOException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();
}   
        }
        if(ins!=null){
        try {
ins.close();
} catch (IOException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();
}   
        }
        if(bouts!=null){
        try {
bouts.close();
} catch (IOException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();
}   
        }
        if(bins!=null){
        try {
bins.close();
} catch (IOException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();
}
        }
       }
       
   }   


/** 
     *  
     * @param inputFileName 输入一个文件夹 
     * @param zipFileName   输出一个压缩文件夹,打包后文件名字 
     * @throws Exception 
     */  
    public void zip(String inputFileName, String zipFileName) throws Exception {  
        zip(zipFileName, new File(inputFileName));  
    }  
  
    private void zip(String zipFileName, File inputFile) throws Exception {  
    ZipOutputStream out=null;
    File file =new File(zipFileName);  
    if(!file.exists()){
try {
boolean mkdirs = file.getParentFile().mkdirs();
boolean createNewFile = file.createNewFile();
if(!mkdirs&&!createNewFile){
throw new BusinessException("生成报文压缩文件错误");
}
} catch (IOException e) {
e.printStackTrace();
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
// 弹出提示信息,并回滚
putError("生成报文压缩文件错误");
}
  }
    FileOutputStream fileOutputStream = new FileOutputStream(file);
   
        try {
out = new ZipOutputStream(fileOutputStream);  
zip(out, inputFile, "");  

} catch (Exception e) {
e.printStackTrace();

finally{
if(out!=null){
out.close();
}
}
    }  
  
    private void zip(ZipOutputStream out, File f, String base)  {  
    FileInputStream in = null;
        try {
if (f.isDirectory()) {  //判断是否为目录   
   File[] fl = f.listFiles();  
   out.putNextEntry(new java.util.zip.ZipEntry(base + "/"));  
   base = base.length() == 0 ? "" : base + "/";  
   for (int i = 0; i < fl.length; i++) {  
       zip(out, fl[i], base + fl[i].getName());  
   }  
} else {                //压缩目录中的所有文件   
   out.putNextEntry(new java.util.zip.ZipEntry(base));  
    in = new FileInputStream(f);  
   int b;  
   while ((b = in.read()) != -1) {  
       out.write(b);  
   }  
   in.close();  
}
} catch (FileNotFoundException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();
} catch (IOException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();

finally{
if(in!=null){

try {
in.close();
} catch (IOException e) {
// 异常日志输出
LogHelper.error(ACTION_NAME, e.toString());
e.printStackTrace();
}  
}
}
    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值