ziputil打包下载

zip打包下载

在这里插入代码片
package com.fc.test.util;


import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Slf4j
public class ZipUtils {

    /**
     * 导出Zip文件
     * @param fileName 文件名称
     * @param fileUrl 需要打包的文件地址
     *
     */
    public static void exportZipFile(String fileName, List<String> fileUrl, HttpServletResponse response, HttpServletRequest request) {

        try {

            // 浏览器处理乱码问题
            String userAgent = request.getHeader("User-Agent");

            // filename.getBytes("UTF-8")处理safari的乱码问题
            byte[] bytes = userAgent.contains("MSIE") ? fileName.getBytes() : fileName.getBytes("UTF-8");

            // 各浏览器基本都支持ISO编码
            fileName = new String(bytes, "ISO-8859-1");

            // 文件名外的双引号处理firefox的空格截断问题
            response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName));
            response.setContentType("application/x-msdownload");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

            generateZipFile(response, fileUrl);

        } catch (Exception e) {
            log.info("下载失败!-->" + e);
        }
    }

    /**
     * 生成Zip文件
     */
    public static void generateZipFile(HttpServletResponse response, List<String> fileUrl) throws IOException {

        ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
        for (int i = 0; i < fileUrl.size(); i++) {
            File file = new File(fileUrl.get(i));
            if(file.exists()){
                String url = fileUrl.get(i);
                //截取文件名称
                String name = url.substring(url.lastIndexOf(File.separator), url.length());

                zipOut.putNextEntry(new ZipEntry(name));

                InputStream stream = getInputStreamByUrl(fileUrl.get(i));

                if (null == stream) {
                    continue;
                }

                int ch;
                byte[] buffer=new byte[1024];
                while((ch=stream.read(buffer))!=-1){
                    zipOut.write(buffer,0,ch);
                }

                stream.close();
            }
        }

        zipOut.closeEntry();
        zipOut.close();
    }

    /**
     * 通过url读取文件信息
     */
    public static InputStream getInputStreamByUrl(String url) {

        InputStream ins = null;

        try {
            /*URL url_ = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) url_.openConnection();
            conn.setConnectTimeout(3 * 1000);
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");*/
            ins = new FileInputStream(new File(url));
        } catch (IOException e) {
            log.info("文件读取失败!-->" + e);
        }

        return ins;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值