首页 > 其他分享 >将本地Excel文件压缩为zip返回下载(解决压缩文件名乱码)

将本地Excel文件压缩为zip返回下载(解决压缩文件名乱码)

时间:2023-02-07 12:11:05浏览次数:49  
标签:文件 zip zipOutputStream 压缩文件 乱码 File path new

 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;
    }

 

 

 

当然,打包下载可以是日志文件等任何文件

 

标签:文件,zip,zipOutputStream,压缩文件,乱码,File,path,new
From: https://www.cnblogs.com/juanxincai/p/17097945.html

相关文章

  • java GZIPOutputStream,GZIPInputStream 用法
    GZIP常常用在linxu环境下,是一种非常简单的压缩算法。在Java实现API中,它仅仅包含两个实现类:GZIPInputStream和GZIPOutputStream。GZIPOutputStream类......
  • 用java实现zip压缩
    本来是写到spaceslive上的,可是代码的显示效果确实不怎么好看。在javaeye上试了试代码显示的顺眼多了。今天写了个用java压缩的功能,可以实现对文件......
  • SpringBoot响应Json数据乱码通过配置解决
    场景实现把SpringBoot的response编码设置为utf-8找到application.properties配置文件添加如下:#设置响应为utf-8spring.http.encoding.force-response=true 再次刷新浏览器......
  • matplotlib中文乱码
    添加两句代码:plt.rcParams['font.sans-serif']=['SimHei']plt.rcParams['axes.unicode_minus']=False #coding=utf-8importmatplotlib.pyplotaspltdecisionNode=......
  • python3中zip详解
    描述zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象......
  • 轻松配置ide启动乱码
    1.设置IDEA启动配置进入idea安装目录D:\ProgramFiles\JetBrains\IntelliJIDEA2018.1.1\bin找到idea64.exe.vmoptions文件添加如下配置-Dfile.encoding=UTF-8注意:配......
  • IIS WordPress 单站点,多站点 中文URL乱码和重定向多次问题解决方法
    前提是需要安装IISURL重写组件(安装方法这里不说明,搜一下资料就有)1、站点网站根目录新增一个chineseurl.php文件用来处理中文url问题<?php//IISMod-Rewriteif(is......
  • 解决执行mvn spring-boot:run报错jar时出错; zip file is empty
    问题描述在执行mvnspring-boot:run的时候,报错[ERROR]读取/Users/diandianxiyu_geek/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.54/tomcat-embed-......
  • geotools读取shp文件中文乱码???
    在读取shp文件之前,先要手动调用一下ShapefileDataStore的setStringCharset方法即可dataStore.setStringCharset(Charset.forName("UTF-8"));参考:https://www.osgeo.cn/p......
  • 【Servlet】Response的OutputStream与Writer输出数据乱码的问题
      OutputStream输出中文数据乱码问题解决方式: packagecn.lsh.servlet;importjava.io.IOException;importjava.io.OutputStream;importjavax.servlet.Se......