package com.ruoyi.common.utils.export;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;

/**
 * @author kai
 */
public class EncodeTools {
    /**
     * 根据浏览器编码文件名 <br>
     *
     * @param fileName 文件名
     * @param request  HttpServletRequest 对象，用来获取浏览器信息
     * @return 编码后的文件名
     * @author Hunter
     * @version [1.0.0, 2015年10月24日]
     * @since [1.0.0]
     */
    public static String encodeFileNameByBrowser(String fileName, HttpServletRequest request) {
        String res = fileName;
        try {
            String userAgent = request.getHeader("USER-AGENT");
            if (StringUtils.contains(userAgent, "Edge") || StringUtils.contains(userAgent, "MSIE")) {
                res = URLEncoder.encode(fileName, "UTF8");
            } else {
                res = new String(fileName.getBytes(), "ISO8859-1");
            }
        } catch (Exception e) {
        }
        return res;
    }
}
