打包下载压缩的文件,照片,影像资料等。

案例示范:

   
            String [] contractNo = this.getRequest().getParameter("contractNos").split(",");
            //将每个资料包压缩成zip包后存放的路径
            String beginZipPath ="/outSrcFileDown/middleFolder/fileDown";
            //在下载文件到本地前先清空该目录并重新建个新目录
            boolean success = deleteDir(new File(beginZipPath));
            //保证有该目录
            File newFile = new File(beginZipPath);
            if(!newFile.exists()){
                newFile.mkdirs();
            };
            
            FileOutputStream fos = null;  
            ZipOutputStream zos = null;
            FileInputStream fis = null;  
            BufferedInputStream bis = null;
        for (int i = 0; i < contractNo.length; i++) {
            //存放每一个合同所有资料包文件路径的集合
            List<String> strs = new ArrayList<String>();
            CsImageQuery query = newQuery(CsImageQuery.class, DEFAULT_SORT_COLUMNS);
            query.setContractNo(contractNo[i]);
            query.setPageSize(Integer.MAX_VALUE);
            Page page = csImageManager.findPage(query);
            if (null != page.getResult() && page.getResult().size() > 0) {
                // 取出所有文件路径
                for (int j = 0; j < page.getResult().size(); j++) {
                    CsImage csmage = (CsImage) page.getResult().get(j);
                    // 判断改文件路径是否存在
                    File fie = new File(csmage.getUrl());
                    if(fie.exists()){
                        strs.add(csmage.getUrl());
                    }
                }
            }
            if (strs.size() > 0) {
                // 将同一个合同的所有资料压缩成zip包存在服务器某个目录
                try {
                    File beginZipFile = new File(beginZipPath + "/" + contractNo[i] + ".zip");
                    if (!beginZipFile.isFile()) {
                        beginZipFile.createNewFile();
                    }
                    fos = new FileOutputStream(beginZipFile);
                    zos = new ZipOutputStream(new BufferedOutputStream(fos));
                    byte[] bufs = new byte[1024 * 10];
                    for (String newStr : strs) {
                        File fie = new File(newStr);
                        if (fie.exists()) {
                            if (fie.isFile()) {
                                // 创建ZIP实体,并添加进压缩包
                                ZipEntry zipEntry = new ZipEntry(fie.getName());
                                zos.putNextEntry(zipEntry);
                                // 读取待压缩的文件并写进压缩包里
                                fis = new FileInputStream(fie);
                                bis = new BufferedInputStream(fis, 1024 * 10);
                                int read = 0;
                                while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
                                    zos.write(bufs, 0, read);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (null != bis)
                        bis.close();
                    if (null != zos)
                        zos.close();
                    fis.close();
                    fos.close();
                }
            }
        }
                //将每个合同的zip打成总的zip包保存到本地
                downBatchOutFiles(this.getResponse(),beginZipPath,"2");

 

 

private void downBatchOutFiles(HttpServletResponse response,String path,String status){
        SimpleDateFormat sif = new SimpleDateFormat("yyyyMMddHHmmss");
        Date da = new Date();
        String zipFileName="";
        if(null!=status && status.equals("1")){
            zipFileName ="委外授权书"+"_"+sif.format(da)+".zip";
        }else{
            zipFileName ="资料包"+"_"+sif.format(da)+".zip";
        }
        
        FileInputStream in = null;
        OutputStream outs = null;
        try{
        ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
        response=this.getResponse();
        response.reset();
        response.setContentType("application/octet-stream;charset=iso-8859-1");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(zipFileName, "UTF-8"));
        // 客户端不缓存
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        List<String> strs = new ArrayList<String>();
        File fil = new File(path);
        File [] filList=fil.listFiles();
        
        //批量下载文件
        for(int j=0;j<filList.length;j++){
            File file = new File(filList[j].toString());
            doZip(file, zos);
        }
        zos.close();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                in.close();
                outs.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
       

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值