Java判断浏览器类型和处理中文乱码问题

1、Java判断浏览器类型

    /** 浏览器类型 */
    public static final byte BROWSER_IE         = 1;
    public static final byte BROWSER_FIREFOX    = 2;
    public static final byte BROWSER_MOZILLA    = 3;
    public static final byte BROWSER_OPERA      = 4;
    public static final byte BROWSER_CHROME     = 5;
    public static final byte BROWSER_ANDROID    = 6;
    public static final byte BROWSER_SAFARI     = 7;
    public static final byte BROWSER_IOS        = 8;
    public static final byte BROWSER_EDGE       = 9;
    public static final byte BROWSER_UNKNOWN    = 0;
 
   /**
     * 获得用户浏览器ua
     * 
     * @param request
     * @return byte 浏览器类型
     */
    public static byte getUserAgent(HttpServletRequest request) {
        String ua = request.getHeader("User-Agent");
        ua = ua.toLowerCase();
        if (ua.indexOf("firefox") > 0) {
            return BROWSER_FIREFOX;
        }
        if (ua.indexOf("opera") > 0) {
            return BROWSER_OPERA;
        }
        if (ua.indexOf("msie") > 0 || ua.indexOf("trident") > 0) {
            return BROWSER_IE;
        }
        if (ua.indexOf("chrome") > 0) {
            return BROWSER_CHROME;
        }
        if (ua.indexOf("android") > 0) {
            return BROWSER_ANDROID;
        }
        if (ua.indexOf("ios") > 0) {
            return BROWSER_IOS;
        }
        if (ua.indexOf("edge") > 0) {
            return BROWSER_EDGE;
        }
        if (ua.indexOf("mac os") > 0 || ua.indexOf("safari") > 0) {
            return BROWSER_SAFARI;
        }
        return BROWSER_UNKNOWN;
    }

2、Java处理中文乱码问题

   /**
     * 处理中文乱码的问题
     * 
     * @param request
     * @param pFileName
     * @return String
     * @throws UnsupportedEncodingException
     */
    // IE浏览器,采用URLEncoder编码
    // Safari浏览器,采用ISO编码的中文输出
    // Chrome浏览器,采用Base64编码或ISO编码的中文输出
    // FireFox浏览器,采用Base64或filename*或ISO编码的中文输出
    public static String encodeChineseFileName(HttpServletRequest request, String pFileName)
            throws UnsupportedEncodingException {
        boolean allIsAscii = true;
        if (pFileName != null) {
            for (int i = 0; i < pFileName.length(); i++) {
                if (pFileName.charAt(i) > 255) {
                    allIsAscii = false;
                    break;
                }
            }
        }
        if (allIsAscii) {
            return pFileName;
        } else {
            String filename = "";
            byte userAgent = getUserAgent(request);
            if (userAgent != BROWSER_UNKNOWN && !StringUtils.isBlank(pFileName)) {
                if (userAgent == BROWSER_FIREFOX) {// firefox
                    String encodeFileName = new String(Base64.encodeBase64(pFileName.getBytes("UTF-8")));
                    filename = "=?UTF-8?B?" + encodeFileName + "?=";
                } else if (userAgent == BROWSER_CHROME) {// chrome
                    filename = new String(pFileName.getBytes(), "ISO8859-1");
                } else if (userAgent == BROWSER_SAFARI) {// Safari
                    filename = new String(pFileName.getBytes(), "ISO8859-1");
                } else {// ie
                    filename = java.net.URLEncoder.encode(pFileName, "UTF-8");
                    filename = org.apache.commons.lang.StringUtils.replace(filename, "+", "%20");// 替换空格
                }
            } else {
                filename = pFileName;
            }
 
            return filename;
        }
    }

Java 
更新时间:2020-05-21 19:36:28

本文由 新逸Cary 创作,如果您觉得本文不错,请随意赞赏
采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
原文链接:https://blog.xinac.cn/archives/200310002.html
最后更新:2020-05-21 19:36:28

评论

Your browser is out of date!

Update your browser to view this website correctly. Update my browser now

×