springboot 多文件压缩下载(流)
zip工具类:
/*** 把文件打成压缩包并输出到客户端浏览器中*/public static void downloadZipFiles(HttpServletResponse response, List<String> srcFiles, String zipFileName) {try {response.reset(); // 重点突出response.setCharacterEncoding("UTF-8"); // 重点突出response.setContentType("application/x-msdownload"); // 不同类型的文件对应不同的MIME类型 // 重点突出// 对文件名进行编码处理中文问题zipFileName = new String(zipFileName.getBytes(), StandardCharsets.UTF_8);// inline在浏览器中直接显示,不提示用户下载// attachment弹出对话框,提示用户进行下载保存本地// 默认为inline方式response.setHeader("Content-Disposition", "attachment;filename=" + zipFileName);// --设置成这样可以不用保存在本地,再输出, 通过response流输出,直接输出到客户端浏览器中。ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());zipFile(srcFiles, zos);} catch (IOException e) {e.printStackTrace();}}/*** 压缩文件** @param filePaths 需要压缩的文件路径集合* @throws IOException*/private static void zipFile(List<String> filePaths, ZipOutputStream zos) {//设置读取数据缓存大小byte[] buffer = new byte[4096];try {//循环读取文件路径集合,获取每一个文件的路径for (String filePath : filePaths) {File inputFile = new File(filePath);//判断文件是否存在if (inputFile.exists()) {//判断是否属于文件,还是文件夹if (inputFile.isFile()) {//创建输入流读取文件BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile));//将文件写入zip内,即将文件进行打包zos.putNextEntry(new ZipEntry(inputFile.getName()));//写入文件的方法,同上int size = 0;//设置读取数据缓存大小while ((size = bis.read(buffer)) > 0) {zos.write(buffer, 0, size);}//关闭输入输出流zos.closeEntry();bis.close();} else { //如果是文件夹,则使用穷举的方法获取文件,写入zipFile[] files = inputFile.listFiles();List<String> filePathsTem = new ArrayList<String>();for (File fileTem : files) {filePathsTem.add(fileTem.toString());}zipFile(filePathsTem, zos);}}}} catch (IOException e) {e.printStackTrace();} finally {if (null != zos) {try {zos.close();} catch (IOException e) {e.printStackTrace();}}}}
controller:
/*** @description: zip* @return: com.ddkj.common.utils.RetMessage* @author: Jay* @date: 2019-12-27 14:15*/@RequestMapping("/packageZip")public RetMessage packageZip(@RequestParam Map<String, Object> params, HttpServletResponse respons){EntityWrapper<PbFileEntity> wrapper = new EntityWrapper<>();if(params.get("repairTaskId") != null ){if(params.get("type") != null){wrapper.eq("type",params.get("type"));wrapper.eq("repair_task_id",params.get("repairTaskId"));List<PbFileEntity> pbFileEntities = pbFileService.selectList(wrapper);List<String> list = new ArrayList<>();if (pbFileEntities != null && pbFileEntities.size()>0) {for (PbFileEntity pbFileEntity : pbFileEntities) {list.add(pbFileEntity.getUrl());}}if(list != null && list.size()>0){FileUtils.downloadZipFiles(respons,list,"image.zip");return null;}}}return RetMessage.error();}
springboot 多文件压缩下载(流)相关推荐
- java实现下载压缩文件_java实现文件压缩下载----压缩下载zip
文件压缩下载 Controller层: /** *文件压缩下载 *billname:文件名 *filename:文件存放路径 */ public void downloadsource(HttpSer ...
- 批量文件压缩下载(zip)
ps:工作之余仅留备份用,未全面完善,请按需取用 一.主要关注两点:1.找到文件并压缩:2.通过文件输出流响应前端 @RestController @RequestMapping("/fil ...
- Java多文件压缩下载解决方案
Java多文件压缩下载解决方案 需求: 会员运营平台经过改版后页面增加了许多全部下载链接,上周上线比较仓促,全部下载是一个直接下载ZIP压缩文件的链接,每个ZIP压缩文件都是由公司运营人员将页面需要下 ...
- php excel模板导出、openoffice excel转pdf、多文件压缩下载
最近两周都在弄关于excel模板导出.excel转pdf.多文件压缩下载.弄得头都大了,接下来说说实现的方法吧. 我用的是laravel5.1的框架,读取模板生成excel,并且插入图片,直接上代码 ...
- java实现文件压缩下载----压缩下载zip
文件压缩下载 Controller层: /** *文件压缩下载 *billname:文件名 *filename:文件存放路径 */ public void downloadsource(HttpSer ...
- java实现文件压缩下载
文件压缩下载 package com.ruoyi.utils; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.uti ...
- Nodejs做后端,实现文件压缩下载的几种方案(archiver、compressing、linux的zip命令):
一. Archiver 1. 安装 npm install archiver 2. 使用 由于需要做文件操作,所以我们需要将fs库也引进来 先创建一个可写流,用于传入压缩包数据 再创建一个archiv ...
- 调用cmd方法,用于网盘文件压缩下载
cmd = cmd.Trim().TrimEnd('&') + "&exit";//说明:不管命令是否成功均执行exit命令,否则当调用ReadToEnd()方法时 ...
- SpringBoot实现多文件压缩下载.zip
前言:服务器生成多个文本文件.需要打包一次返回下载.例如:xml,word,excel try {response.setHeader("Content-Disposition", ...
最新文章
- python 根据地址求经纬度 谷歌_利用google的API获取世界城市经纬度(python实现)
- StringUtils 正则校验
- Stack:删除并返回栈顶元素?
- Spark SQL 1.x之Hive Context
- 标准C语言库 Glibc 2.15
- Java Vector setElementAt()方法与示例
- (11)FPGA跨时钟域问题导致数据偶尔异常(学无止境)
- 51Nod-1091 线段的重叠【排序】
- python笔记(一)获取当前目录路径和文件(抄录)
- Hadoop的shell命令
- 无源贴片晶振四角引脚_如何区分贴片晶振的脚位方向
- 辛普森 matlab,利用MATLAB软件编写辛普森求积公式程序
- 税费计算机,友商税费计算器
- 5个增加设计趣味性的方法
- 3825. 逃离大森林
- 百果园港交所上市:市值近百亿港元 80%营收来自加盟店
- cdp4j The status code of the opening handshake response is not ‘101 Switching Protocols‘
- activiti的流程事件
- element日历上月下月样式修改
- iphonex美版型号对照表_iPhoneX国行和美版/港版/日版哪个好?苹果X各国家版本区别对比...