package com.jeeplus.test.reimbursementsys.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 本地服务器文件下载工具类
 */
public class ThisLocalityDownloadUtil {

    public void download(String fileName, HttpServletRequest request, HttpServletResponse response) {
        try {
            //读取服务器的文件下载路径
            String paths = this.getClass().getResource("/").getPath();
            String filePath = paths.substring(1, paths.length() - 16);
            System.out.println("===========os.name:"+System.getProperties().getProperty("os.name"));
            //判定当前系统是否为linux版本
            Boolean isWindows = System.getProperty("os.name").toLowerCase().contains("linux");
            String path = "";
            if(!isWindows){
                path = filePath+"/dot/"+fileName;
            }else{
                //如果是linux系统 则需要在路径前加上一个 /
                path = "/"+filePath+"/dot/"+fileName;
            }
            downCfg(fileName, request, response);
            OutputStream out;
            FileInputStream inputStream = new FileInputStream(path);
            out = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            inputStream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void downCfg(String fileName, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        // 判断浏览器，进行不同的加密，这样下载的时候保存的文件名就不会乱码
        String userAgent = request.getHeader("User-Agent");
        // 针对IE或者以IE为内核的浏览器：
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } else {
            // 非IE浏览器的处理：
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName));
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
    }

    public void downloadFileTemplate(String fileName, HttpServletRequest request, HttpServletResponse response) {
        try {
            //读取服务器的文件下载路径
            String paths = this.getClass().getResource("/").getPath();
            String filePath = paths.substring(1, paths.length() - 16);
            System.out.println("===========os.name:"+System.getProperties().getProperty("os.name"));
            String path = "";
            path = this.getClass().getResource("/").getPath()+"template/"+fileName;
            downCfg(fileName, request, response);
            OutputStream out;
            FileInputStream inputStream = new FileInputStream(path);
            out = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            inputStream.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
