• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

文件压缩下载,Feign文件流传递

武飞扬头像
阿小木的愤怒
帮助1

1、文件下载

将 HttpServletResponse对象通过接口参数传递至后端代码,然后在HttpServletResponse对象中获取到返回给前端的文件输出流:

OutputStream out = httpServletResponse.getOutputStream();

将需要下载的文件通过输出流返回至前端。

  1.  
    @Override
  2.  
    public void downloadFile(String fileId, String componentId, String folderName, HttpServletResponse response) {
  3.  
    WorkPaperCatalogDO workPaperCatalogDO = workPaperCatalogService.getFile(componentId, fileId, false);
  4.  
    if (workPaperCatalogDO == null) {
  5.  
    return;
  6.  
    }
  7.  
     
  8.  
    try (InputStream inputStream = fileStorageService.getFile(componentId, workPaperCatalogDO.getFolderName(), workPaperCatalogDO.getFileId())) {
  9.  
    if (inputStream == null) {
  10.  
    return;
  11.  
    }
  12.  
     
  13.  
    response.setCharacterEncoding("utf-8");
  14.  
    response.setContentType("application/octet-stream");
  15.  
    response.setHeader("Content-Disposition",
  16.  
    "attachment;fileName=" URLEncoder.encode(workPaperCatalogDO.getFileName(), "UTF-8"));
  17.  
    response.setHeader("Access-Control-Expose-Headers", "*");
  18.  
    IOUtils.copy(inputStream, response.getOutputStream());
  19.  
    } catch (IOException e) {
  20.  
    e.printStackTrace();
  21.  
    }
  22.  
    }
学新通

2、压缩导出

通过使用ZipOutputStream对象,将文件进行压缩,然后导出

  1.  
    @ApiOperation("下载文件")
  2.  
    @RequestMapping(value = "generateAuditProcFile", method = RequestMethod.GET)
  3.  
    public void editAuditProcedure(HttpServletResponse httpServletResponse, @RequestParam String componentId) throws IOException {
  4.  
     
  5.  
    String fileName = "测试压缩文件.zip";
  6.  
    httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" URLEncoder.encode(fileName, "UTF-8"));
  7.  
    ByteArrayOutputStream byteArrayOutputStream = auditProcedureService.generateAuditProcFile(componentId);
  8.  
    if(byteArrayOutputStream == null) {
  9.  
    throw new BusinessException("获取审计程序文件失败");
  10.  
    }
  11.  
    //转换压缩文件
  12.  
    ZipOutputStream zipOutputStream = new ZipOutputStream(httpServletResponse.getOutputStream());
  13.  
    zipOutputStream.putNextEntry(new ZipEntry("测试压缩文件.xlsx"));
  14.  
    InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
  15.  
    IOUtils.copy(inputStream, zipOutputStream);
  16.  
    inputStream.close();
  17.  
    zipOutputStream.closeEntry();
  18.  
    zipOutputStream.finish();
  19.  
    zipOutputStream.flush();
  20.  
    }
学新通

3、通过Feign调用传递输出流

定义Feign接口,该接口返回参数定义 Response,来自feign.Response,定义该接口时,不需要关注对方接口的HttpServletResponse参数。

  1.  
    @FeignClient(name = "swworkpaper", url = "${gsum.service.workpaper_url}")
  2.  
    public interface WorkpaperFeignService {
  3.  
    @RequestMapping(value = "/api/auditProcedureTailor/generateAuditProcFile",method = RequestMethod.GET)
  4.  
    Response generateAuditProcFile(@RequestParam("componentId") String componentId);
  5.  
    }

调用该接口后,收到Response返回对象,将该对象中的body数据转为输入流

  1.  
    //调用Feign接口,获取输出流到Response对象
  2.  
    Response fileInputStream = workpaperFeignService.generateAuditProcFile(componentId);
  3.  
    //将返回流中的body数据转为输入流
  4.  
    inputStream = fileInputStream.body().asInputStream();
  5.  
    //输入流转为压缩数据流
  6.  
    ZipInputStream zipInputStream = new ZipInputStream(inputStream);
  7.  
    //可以进行解压处理,或者进行数据下载,此处进行解压处理,然后统一进行压缩,下载
  8.  
    ZipEntry entry;
  9.  
    while ((entry = zipInputStream.getNextEntry()) != null){
  10.  
    log.info("unziping:" entry);
  11.  
    zipOutputStream.putNextEntry(entry);
  12.  
    byte buf[] = new byte[1024];
  13.  
    int length;
  14.  
    while ((length = zipInputStream.read(buf)) > 0) {
  15.  
    zipOutputStream.write(buf, 0, length);
  16.  
    }
  17.  
    zipOutputStream.closeEntry();
  18.  
    }
  19.  
     
  20.  
     
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbchkc
系列文章
更多 icon
同类精品
更多 icon
继续加载