java 导出压缩包方法

直接上java代码

@RequestMapping(params = "exportPicZIP")
    public void exportPicZIP(HttpServletRequest request,
            HttpServletResponse response) throws IOException, InvalidFormatException {
         List<String> idses = new ArrayList<String>();
         String bfmfmSiteBookEntity_companyname = "";
         if(StringUtils.isNotEmpty(request.getParameter("bfmfmSiteBookEntity_companyname"))) {
                 bfmfmSiteBookEntity_companyname = new String(request.getParameter("bfmfmSiteBookEntity_companyname").getBytes("iso-8859-1"), "utf-8"); 
         }
        String opionstatus = request.getParameter("opionstatus");
        String jcid = request.getParameter("jcid");
        String ids = request.getParameter("ids");
        if(StringUtils.isNotEmpty(ids)) {
            String id[] = ids.split(",");  
            for (int i = 0; i < id.length; i++) {
                idses.add(id[i]);
            }
        }
        ForthetimeEntity forthetimeEntity = systemService.getEntity(ForthetimeEntity.class, jcid);
        CriteriaQuery cq = new CriteriaQuery(ActivityplanEntity.class);
        //查询条件组装器
        cq.createAlias("bfmfmSiteBookEntity", "bfmfmSiteBookEntity");
        cq.eq("jcid",forthetimeEntity.getId());
        if(idses.size() >0) {
            cq.in("id", idses.toArray());
        }else {
            if(StringUtils.isNotEmpty(bfmfmSiteBookEntity_companyname)) {
                cq.like("bfmfmSiteBookEntity.companyname", "%"+bfmfmSiteBookEntity_companyname+"%");
            }
            if(StringUtils.isNotEmpty(opionstatus)) {
                cq.eq("opionstatus", opionstatus);
            }else {
                cq.eq("opionstatus", "3");
            }
        }
        cq.eq("status", "0");
        cq.add();
        List<ActivityplanEntity> plants  = this.systemService.getListByCriteriaQuery(cq, false);
        
        List<ActivitypersonEntity> persons = new ArrayList<ActivitypersonEntity>();
        for (ActivityplanEntity plan : plants) {
            persons.addAll(plan.getActivitypersonEntitys());
        }
        PropertiesUtil util = new PropertiesUtil("sysConfig.properties");
         byte[] buffer = new byte[1024];
        String strZipPath = util.readProperty("exportPlanPicPath");
         File zipFile = new File(strZipPath);
         if(!zipFile.exists()) {
             zipFile.mkdirs();
         }
         String strDate=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());//文件保存进来,重新命名。
         String fileZipName = strDate+".zip";
         String filenameTemp = zipFile.getAbsolutePath()+"\\"+fileZipName;//文件路径+名称+文件类型
         File file = new File(filenameTemp);
         if(!file.exists()){
             file.createNewFile();
         }
         ZipOutputStream out = null;
          out = new ZipOutputStream(new FileOutputStream(filenameTemp));
          out.setEncoding("gbk");
             File[] filesz = new File[persons.size()];
            String path = "";
            String fileName = "";
            String filepath = util.readProperty("webUploadpath");
            int i = 0;
            for (ActivitypersonEntity person : persons) {
                 path = filepath+"\\"+person.getPhotopath();
                 String picname = person.getPhotoname();
                 String suffix = picname.substring(picname.lastIndexOf(".") + 1);
                String idcardNum = person.getDocumentnumber();
                fileName = idcardNum+"."+suffix;
                filesz[i] = new File(path);
                FileInputStream fis = new FileInputStream(filesz[i]);
                   out.putNextEntry(new ZipEntry(fileName));
                    int len;
                    while ((len = fis.read(buffer)) > 0) {
                 out.write(buffer, 0, len);
            }
                  out.closeEntry();
                  fis.close();
                  i++;
            }
              out.close();
        // 设置Content-Disposition
            FileInputStream fis = null;
            OutputStream os = null;
            try {
                response.addHeader("Content-Disposition",
                        "attachment;filename=" + new String(fileZipName.getBytes(), "iso-8859-1"));
                File fileZip = new File(filenameTemp);
                if (fileZip.exists()) {
                    response.addHeader("Content-Length", "" + fileZip.length());
                    fis = new FileInputStream(filenameTemp);
                    os = response.getOutputStream();
                    int count = 0;
                    byte[] buffer1 = new byte[1024 * 1024];
                    while ((count = fis.read(buffer1)) != -1)
                        os.write(buffer1, 0, count);
                    os.flush();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (os != null)
                    try {
                        os.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                if (fis != null)
                    try {
                        fis.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                
                File excelFiles = new File(filenameTemp);
                if(excelFiles.exists()) {
                    excelFiles.delete();
                }
            }
      }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值