package com.ssm.manage.utils;



import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class ExcelUtils {


    //   导出本地文件
    public static void downloadExcelFile(String templateFileName, String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", FileUtils.setFileDownloadHeader(request, fileName + ".jpg"));
//            复制
            IOUtils.copy(new FileInputStream(new File(new File("").getCanonicalPath() + "\\" + templateFileName)), response.getOutputStream(), 1024);
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception("下载图片失败");
        }
    }
    public static void downloadExcelTemplate(String templateFileName, String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
        BufferedInputStream bis = null;
        InputStream in = null;
        try {
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + FileUtils.setFileDownloadHeader(request, fileName + ".jpg"));
            OutputStream out = response.getOutputStream();
//            in = EasyExcelUtil.class.getClassLoader().getResourceAsStream(templateFileName);
            in = new FileInputStream(new File(new File("").getCanonicalPath() + "\\" + templateFileName));
            bis = new BufferedInputStream(in);
            byte[] buff = new byte[1024];
            int i = bis.read(buff);
            while (i != -1) {
                out.write(buff, 0, buff.length);
                i = bis.read(buff);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception("下载模板失败");
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     *
     * @param dirPath
     *          文件所在的路径
     */
    public static void circleMethod(String dirPath) {
        File file = new File(dirPath);
        if (file.isDirectory()) {//if(file.isDirectory)；如果path表示的是一个目录则返回true。
            String[] dirPathList = file.list();
            for (int i = 0; i < dirPathList.length; i++) {
                String filePath = dirPath + File.separator + dirPathList[i];
                File fileDelete = new File(filePath);
                if (fileDelete.getName().equals("1657010489390.jpg")) {
                    fileDelete.delete();
                }
                //circleMethod(filePath);
            }
        }
    }

}
