文件压缩下载,Feign文件流传递
1、文件下载
将 HttpServletResponse对象通过接口参数传递至后端代码,然后在HttpServletResponse对象中获取到返回给前端的文件输出流:
OutputStream out = httpServletResponse.getOutputStream();
将需要下载的文件通过输出流返回至前端。
-
-
public void downloadFile(String fileId, String componentId, String folderName, HttpServletResponse response) {
-
WorkPaperCatalogDO workPaperCatalogDO = workPaperCatalogService.getFile(componentId, fileId, false);
-
if (workPaperCatalogDO == null) {
-
return;
-
}
-
-
try (InputStream inputStream = fileStorageService.getFile(componentId, workPaperCatalogDO.getFolderName(), workPaperCatalogDO.getFileId())) {
-
if (inputStream == null) {
-
return;
-
}
-
-
response.setCharacterEncoding("utf-8");
-
response.setContentType("application/octet-stream");
-
response.setHeader("Content-Disposition",
-
"attachment;fileName=" URLEncoder.encode(workPaperCatalogDO.getFileName(), "UTF-8"));
-
response.setHeader("Access-Control-Expose-Headers", "*");
-
IOUtils.copy(inputStream, response.getOutputStream());
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
2、压缩导出
通过使用ZipOutputStream对象,将文件进行压缩,然后导出
-
-
-
public void editAuditProcedure(HttpServletResponse httpServletResponse, String componentId)throws IOException {
-
-
String fileName = "测试压缩文件.zip";
-
httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" URLEncoder.encode(fileName, "UTF-8"));
-
ByteArrayOutputStream byteArrayOutputStream = auditProcedureService.generateAuditProcFile(componentId);
-
if(byteArrayOutputStream == null) {
-
throw new BusinessException("获取审计程序文件失败");
-
}
-
//转换压缩文件
-
ZipOutputStream zipOutputStream = new ZipOutputStream(httpServletResponse.getOutputStream());
-
zipOutputStream.putNextEntry(new ZipEntry("测试压缩文件.xlsx"));
-
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
-
IOUtils.copy(inputStream, zipOutputStream);
-
inputStream.close();
-
zipOutputStream.closeEntry();
-
zipOutputStream.finish();
-
zipOutputStream.flush();
-
}
3、通过Feign调用传递输出流
定义Feign接口,该接口返回参数定义 Response,来自feign.Response,定义该接口时,不需要关注对方接口的HttpServletResponse参数。
-
-
public interface WorkpaperFeignService {
-
-
Response generateAuditProcFile(; String componentId)
-
}
调用该接口后,收到Response返回对象,将该对象中的body数据转为输入流
-
//调用Feign接口,获取输出流到Response对象
-
Response fileInputStream = workpaperFeignService.generateAuditProcFile(componentId);
-
//将返回流中的body数据转为输入流
-
inputStream = fileInputStream.body().asInputStream();
-
//输入流转为压缩数据流
-
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
-
//可以进行解压处理,或者进行数据下载,此处进行解压处理,然后统一进行压缩,下载
-
ZipEntry entry;
-
while ((entry = zipInputStream.getNextEntry()) != null){
-
log.info("unziping:" entry);
-
zipOutputStream.putNextEntry(entry);
-
byte buf[] = new byte[1024];
-
int length;
-
while ((length = zipInputStream.read(buf)) > 0) {
-
zipOutputStream.write(buf, 0, length);
-
}
-
zipOutputStream.closeEntry();
-
}
-
-
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgbchkc
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01