Spring Boot 单文件下载 , 多文件 zip 下载

Spring Boot 单文件下载 , 多文件 zip 下载


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
 * @program: FILE
 * @description:
 */
@RestController
@RequestMapping("/file")
public class FileController {

    /**
     * 下载
     *
     * @param response
     */
    @GetMapping("/download")
    public void zipDownload(HttpServletResponse response) {
        // , "实体类格式化-2.txt", "实体类格式化-3.txt"
        String fileNameArr[] = {"实体类格式化-1.txt"};
        // , "C:/file/实体类格式化-2.txt", "C:/file/实体类格式化-3.txt"
        String filePathArr[] = {"C:/file/实体类格式化-1.txt"};
        if (fileNameArr.length > 1) {
            zipFile(response, fileNameArr, filePathArr);
        } else {
            oneFile(response, fileNameArr[0], filePathArr[0]);
        }
    }

    /**
     *
     * @param response
     * @param fileNameArr
     * @param filePathArr
     */
    public void zipFile(HttpServletResponse response, String fileNameArr[], String filePathArr[]) {
        // 保存 zip 文件的临时目录
        String directory = "C:/file";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HHmmss");
        String zipFileName = formatter.format(new Date()) + ".zip"; // 设置 zip 文件名称
        String strZipPath = directory + "/" + zipFileName;
        ZipOutputStream zipStream = null;
        FileInputStream zipSource = null;
        BufferedInputStream bufferStream = null;
        File zipFile = new File(strZipPath);
        try {
            zipStream = new ZipOutputStream(new FileOutputStream(zipFile));
            for (int i = 0; i < filePathArr.length; i++) {
                String realFileName = fileNameArr[i];
                String realFilePath = filePathArr[i];
                File file = new File(realFilePath);
                if (file.exists()) {
                    zipSource = new FileInputStream(file);
                    ZipEntry zipEntry = new ZipEntry(realFileName);
                    zipStream.putNextEntry(zipEntry);
                    bufferStream = new BufferedInputStream(zipSource, 1024 * 10);
                    int read = 0;
                    byte[] buf = new byte[1024 * 10];
                    while ((read = bufferStream.read(buf, 0, 1024 * 10)) != -1) {
                        zipStream.write(buf, 0, read);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bufferStream) bufferStream.close();
                if (null != zipStream) {
                    zipStream.flush();
                    zipStream.close();
                }
                if (null != zipSource) zipSource.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (zipFile.exists()) {
            oneFile(response, zipFileName, strZipPath);
            // 删除 zip 文件
            zipFile.delete();
        }
    }

    /**
     *
     * @param response
     * @param filename
     * @param path
     */
    public void oneFile(HttpServletResponse response, String filename, String path) {
        if (filename != null) {
            FileInputStream is = null;
            BufferedInputStream bs = null;
            OutputStream os = null;
            try {
                File file = new File(path);
                if (file.exists()) {
                    //设置 Headers 请求头 application/octet-stream (二进制流,不知道下载文件类型)
                    response.setHeader("Content-Type", "application/octet-stream");
                    response.setHeader("Content-Disposition", "attachment;filename=" +
                    new String(filename.getBytes("UTF-8"), "ISO8859-1"));
                    is = new FileInputStream(file);
                    bs = new BufferedInputStream(is);
                    os = response.getOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = bs.read(buffer)) != -1) {
                        os.write(buffer, 0, len);
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                    if (bs != null) {
                        bs.close();
                    }
                    if (os != null) {
                        os.flush();
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值