package com.zksy.utils;


import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.util.FileCopyUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class DowntemplateUtil {

    private final static String FILE_PATH = "template/";
    private final static String FILE_SUFFIX = ".xlsx";
    private final static String FILE_WORD = ".doc";
    /**
     * 给一个请求体对象再给个template下的文件   后缀需要为.xlsx的文件 就能给前端下载了
     * @param response
     * @param fileName
     * @throws IOException
     */
    public static void downloadTemplate(HttpServletResponse response,String fileName) throws IOException {
        ClassPathResource resource = new ClassPathResource(FILE_PATH + fileName + FILE_SUFFIX);
        if (!resource.exists()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "没找到这个: " + FILE_PATH + fileName + FILE_SUFFIX + "文件");
            throw new IOException("没找到这个: " + FILE_PATH + fileName + FILE_SUFFIX + "文件");
        }

        // 设置响应头以支持文件下载
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + new String((fileName + FILE_SUFFIX).getBytes("UTF-8"), "ISO-8859-1"));
        headers.add(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel");

        // 从资源读取输入流并写入响应的输出流
        try (InputStream inputStream = resource.getInputStream();
             OutputStream outputStream = response.getOutputStream()) {

            response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(resource.contentLength()));

            FileCopyUtils.copy(inputStream, outputStream);
            outputStream.flush();
        }
    }
    /**
     * 给一个请求体对象再给个template下的文件   后缀需要为.xlsx的文件 就能给前端下载了
     * @param response
     * @param fileName
     * @throws IOException
     */
    public static void downloadTemplateWord(HttpServletResponse response,String fileName) throws IOException {
        ClassPathResource resource = new ClassPathResource(FILE_PATH + fileName + FILE_WORD);
        if (!resource.exists()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "没找到这个: " + FILE_PATH + fileName + FILE_WORD + "文件");
            throw new IOException("没找到这个: " + FILE_PATH + fileName + FILE_WORD + "文件");
        }

        // 设置响应头以支持文件下载
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + new String((fileName + FILE_WORD).getBytes("UTF-8"), "ISO-8859-1"));
        headers.add(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel");

        // 从资源读取输入流并写入响应的输出流
        try (InputStream inputStream = resource.getInputStream();
             OutputStream outputStream = response.getOutputStream()) {

            response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(resource.contentLength()));

            FileCopyUtils.copy(inputStream, outputStream);
            outputStream.flush();
        }
    }
}
