java 打包下载

前台js

var _downloadZipAjax = function(obj,url){
   var elemIF = document.createElement("iframe");
   elemIF.src = url;
   elemIF.style.display = "none";
   document.body.appendChild(elemIF);
};

后台

@RequestMapping(value = "/downloadZipAjax")
public ResponseEntity<byte[]> downloadZipAjax(String period, String cNo) throws Exception {
   ZipTool zt = new ZipTool();
   ReportErrorInfo reportErrorInfo = new ReportErrorInfo();
   reportErrorInfo.setNo(cNo);
   reportErrorInfo.setPeriod(period);
   reportErrorInfo = reportErrorInfoFeign.getByErrorInfo(reportErrorInfo);
   //zu.addFile(filepath);//添加文件
   if(reportErrorInfo.getFileAddr() != null && !"".equals(reportErrorInfo.getFileAddr())){
      String path = UploadUtil.getFileUploadPath();
      String[] fileAddrs = reportErrorInfo.getFileAddr().split(",");
      for(int i =0;i<fileAddrs.length;i++){
         zt.addFile(path+fileAddrs[i]);
      }
   }
   
   String fileName = "AA企业"+"_"+ WaterIdUtil.getWaterId()+".zip";
   fileName = new String(fileName.getBytes(),"ISO8859-1");
   InputStream inputStream = new ByteArrayInputStream(zt.getOutputStream());
   HttpHeaders headers = new HttpHeaders();
   headers.add("Content-Disposition", "attchement;filename="+fileName);
   HttpStatus statusCode = HttpStatus.OK;
   ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(toByteArray(inputStream), headers, statusCode);
   return entity;
}

 

ZipTool 是一个封装的工具类

public class ZipTool {
    
   private ByteArrayOutputStream outputStream = null;
   private ZipOutputStream zip = null;
   
   public ZipTool() {
      outputStream = new ByteArrayOutputStream();
      zip = new ZipOutputStream(outputStream);
   }
   /**
    * 添加文件到压缩包
    * @param filepath
    */
   public void addFile(String filepath) {
      File file = new File(filepath);
      String filename = file.getName();
      try {
         zip.putNextEntry(new ZipEntry(filename));
         byte[] filebyte = file2byte(file);
         IOUtils.write(filebyte, zip);
         zip.closeEntry();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   
   /**
    * 添加文件到压缩包,并指定新的文件名
    * @param filepath
    */
   public void addFileReName(String filepath,String NewFileName) {
      File file = new File(filepath);
      try {
         zip.putNextEntry(new ZipEntry(NewFileName));
         byte[] filebyte = file2byte(file);
         IOUtils.write(filebyte, zip);
         zip.closeEntry();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   
   /**
    * 添加文件到压缩包,并指定压缩包内路径
    * @param filepath
    * @param inZipPath
    */
   public void addFile(String filepath,String inZipPath) {
      File file = new File(filepath);
      try {
         zip.putNextEntry(new ZipEntry(inZipPath));
         byte[] filebyte = file2byte(file);
         IOUtils.write(filebyte, zip);
         zip.closeEntry();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   /**
    * 将压缩包生成到指定位置
    * @param targetFileName
    */
   public void outZip(String targetFileName) {
      String fileOutName = targetFileName + ".zip";
      FileOutputStream fos = null;
      try {
         fos = new FileOutputStream(fileOutName);
         IOUtils.closeQuietly(zip);
         IOUtils.write(outputStream.toByteArray(), fos);
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }finally {
         close();
      }

   }
   /**
    * File 转换成 byte
    * @param file
    * @return
    */
   public static byte[] file2byte(File file) {
      byte[] buffer = null;
      try {
         FileInputStream fis = new FileInputStream(file);
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         byte[] b = new byte[1024];
         int n;
         while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
         }
         fis.close();
         bos.close();
         buffer = bos.toByteArray();
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      return buffer;
   }
   /**
    * 获取输出流字节
    * @return byte[]
    */
   public byte[] getOutputStream() {
      IOUtils.closeQuietly(zip);
      byte[] byteArray = outputStream.toByteArray();
      close();
      return byteArray;
   }
   /**
    * 关闭流
    */
   public void close() {
      try {
         if (outputStream != null) {
            outputStream.close();

         }
         if (zip != null) {
            zip.close();
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }


   public static void main(String[] args) {
      ZipTool zt = new ZipTool();

      zt.close();
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值