下载多个文件压缩

下载单个文件

List<FindResultCodeResponse> resultCodeResponseList = resultCodeMapper.getResultCodes(resultCodeRequest);
String resultCodes = JSON.toJSONString(resultCodeResponseList);
response.addHeader("Content-Disposition", "attachment;filename=resultCode.java");
response.setContentType("application/csv;charset=UTF-8");
PrintWriter out = null;
try {
  out = response.getWriter();
  out.write(resultCodes);
} catch (IOException e) {
  e.printStackTrace();
  CustomLoggerUtil.printErrorLog(this.getClass(),"downloadNacosFile-下载nacos文件异常=[{}]",e);
}
out.flush();

下载多个文件压缩

public void downloadFile(HttpServletResponse response, List<File> files){
    response.setHeader("Content-Disposition", "attachment;fileName=ResultCodeEnum.zip");
    OutputStream outputStream = null;
    ZipOutputStream zos = null;
    try {
      outputStream = response.getOutputStream();
      zos = new ZipOutputStream(outputStream);
      // 将文件流写入zip中
      downloadTolocal(zos,files);

    } catch (IOException e) {
      CustomLoggerUtil.printErrorLog(this.getClass(),"downloadEnumFile-下载全部文件失败,错误信息=[{}]",e);
    }finally {
      if(zos != null) {
        try {
          zos.close();
        } catch (Exception e2) {
          CustomLoggerUtil.printInfo(this.getClass(),"关闭输入流时出现错误",e2);
        }
      }
      if(outputStream != null) {
        try {
          outputStream.close();
        } catch (Exception e2) {
          CustomLoggerUtil.printInfo(this.getClass(),"关闭输入流时出现错误",e2);
        }
      }

    }

  }


private void downloadTolocal(ZipOutputStream zos,List<File> files) throws IOException {

    for (File file : files) {
      //文件名称(带后缀)
      String fileName = file.getName();
      InputStream is = null;
      BufferedInputStream in = null;
      byte[] buffer = new byte[1024];
      int len;
      //创建zip实体(一个文件对应一个ZipEntry)
      ZipEntry entry = new ZipEntry(fileName);
      try {
        //获取需要下载的文件流
        is = new BufferedInputStream(new FileInputStream(file));
        in = new BufferedInputStream(is);
        zos.putNextEntry(entry);
        //文件流循环写入ZipOutputStream
        while ((len = in.read(buffer)) != -1 ) {
          zos.write(buffer, 0, len);
        }
      } catch (Exception e) {
        CustomLoggerUtil.printInfo(this.getClass(),"下载全部文件--压缩文件出错",e);
      }finally {
        if(entry != null) {
          try {
            zos.closeEntry();
          } catch (Exception e2) {
            CustomLoggerUtil.printInfo(this.getClass(),"下载全部文件--zip实体关闭失败",e2);
          }
        }
        if(in != null) {
          try {
            in.close();
          } catch (Exception e2) {
            CustomLoggerUtil.printInfo(this.getClass(),"下载全部文件--文件输入流关闭失败",e2);
          }
        }
        if(is != null) {
          try {
            is.close();
          }catch (Exception e) {
            CustomLoggerUtil.printInfo(this.getClass(),"下载全部文件--输入缓冲流关闭失败",e);
          }
        }
      }
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值