java多个文件下载之压缩包下载

  @Override
    public void exportCertificate( HttpServletRequest request, HttpServletResponse response) {
        try {
          
          //根据自己需求查询组装文件路径
            List<String>   list = ArrayList();
            list.add("文件路径1");
             list.add("文件路径2")
             //.....
           


            response.setHeader("Content-Disposition", "attachment;filename=" + new String("压缩包文件名称.zip".getBytes("GB2312"), "ISO-8859-1"));  // 需要编码否则中文乱码
            response.setContentType("application/zip;charset=utf-8");
            response.setCharacterEncoding("UTF-8");

            // 输出流直接用ZipOutputStream包裹,这样直接输出压缩后的流。减少服务器生成压缩文件步骤。
            ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());





            
             if(list!=null && list.size()>0){
                 for(String v:list){
                 //判断是否为空
                     if(StringUtils.isNotEmpty(v)){


                         File file=new File(s);
                         //判断文件是否存在
                          if(file.exists()){
                              //压缩包里面的文件名
                              ZipEntry zipEntryXtv = new ZipEntry(file.getName());
                              zipOutputStream.putNextEntry(zipEntryXtv);

                               //将当前文件写入压缩包中
                              FileInputStream IN = new FileInputStream(file);
                              BufferedInputStream bins = new BufferedInputStream(IN, 512);
                              int nNumber;
                              byte[] buffer = new byte[512];
                              //写入压缩包中
                              while ((nNumber = bins.read(buffer)) != -1) {
                                  zipOutputStream.write(buffer, 0, nNumber);
                              }
                              bins.close();
                              IN.close();
                          }

                      }
                 }
             }
            zipOutputStream.close();

        }catch (Exception e){
            log.info("报错:"+e.getMessage());
        }


    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值