将本地Excel文件压缩为zip返回下载(解决压缩文件名乱码)
String zipPath =basePath+File.separator+"ZIP"+File.separator; File zip = new File(basePath+File.separator+"ZIP"+File.separator+fileName+Time+".zip"); filesToZip(srcfile, zip); response.setContentType("application/octet-stream"); response.setHeader("Location", zip.getName()); String fileName = new String(zip.getName().getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); try { OutputStream outputStream = response.getOutputStream(); InputStream inputStream = new FileInputStream(zip); byte[] buffer = new byte[1024]; int i = -1; while ((i = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, i); } inputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { delAllFile(zipPath); }
将日期段内的数据按模板生成Excel,存放于日期文件夹下即可,网上有很多生成Excel的代码,这里略过
本地按日期生成的Excel,路径分层 /日期/文件/ 前端访问带起始和结束时间,计算天数然后查回文件,将文件放集合再转为File[],调用filesToZip来压缩为一个压缩包,返回给response的outputStream 即可
public void filesToZip(File[] srcFiles, File zipFile) { // 判断压缩后的文件存在不,不存在则创建 if (!zipFile.exists()) { try { zipFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } // 创建 FileOutputStream 对象 FileOutputStream fileOutputStream = null; // 创建 ZipOutputStream ZipOutputStream zipOutputStream = null; // 创建 FileInputStream 对象 FileInputStream fileInputStream = null; try { // 实例化 FileOutputStream 对象 fileOutputStream = new FileOutputStream(zipFile); // 实例化 ZipOutputStream 对象 zipOutputStream = new ZipOutputStream(fileOutputStream); // 创建 ZipEntry 对象 ZipEntry zipEntry = null; // 遍历源文件数组 for (int i = 0; i < srcFiles.length; i++) { // 将源文件数组中的当前文件读入 FileInputStream 流中 fileInputStream = new FileInputStream(srcFiles[i]); // 实例化 ZipEntry 对象,源文件数组中的当前文件 zipEntry = new ZipEntry(srcFiles[i].getName()); zipOutputStream.putNextEntry(zipEntry); // 该变量记录每次真正读的字节个数 int len; // 定义每次读取的字节数组 byte[] buffer = new byte[1024]; while ((len = fileInputStream.read(buffer)) > 0) { zipOutputStream.write(buffer, 0, len); } } zipOutputStream.closeEntry(); zipOutputStream.close(); fileInputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
最后将生成的临时zip文件删除
public static boolean delAllFile(String path) { boolean flag = false; File file = new File(path); if (!file.exists()) { return flag; } if (!file.isDirectory()) { return flag; } String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { temp.delete(); } if (temp.isDirectory()) { delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件 flag = true; } } return flag; }
当然,打包下载可以是日志文件等任何文件
学习时的痛苦是暂时的 未学到的痛苦是终生的
作者:卷心菜的奇妙历险
本文版权归作者和博客园共有,遵循 CC 4.0 BY-SA 版权协议,欢迎转载 转载请附上原文出处链接和本声明,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
2020-02-07 IDEA 解决Number objects are compared using '==', not 'equals()' 警告
2020-02-07 IDEA 找不到包或者找不到符号的一些解决办法