将本地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;
    }

 

 

 

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

 

posted @ 2023-02-07 12:04  卷心菜的奇妙历险  阅读(189)  评论(0编辑  收藏  举报