获取浏览器类型工具类

获取浏览器类型工具类


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Created by 谭健 on 2018/4/16 0016. 星期一. 16:49.
 * © All Rights Reserved.
 * <p>
 * <p>
 * 浏览器工具类
 */
public class BrowserUtils {


    /**
     *
     * @param requestUserAgent = request.getHeader("USER-AGENT")
     * @return Browser 类型枚举
     */
    public static Browser getBrowser(String requestUserAgent) {


        if (distinguishAgent(requestUserAgent, IE11)) { return Browser.IE11; }
        if (distinguishAgent(requestUserAgent, IE10)) { return Browser.IE10; }
        if (distinguishAgent(requestUserAgent, IE9)) { return Browser.IE9; }
        if (distinguishAgent(requestUserAgent, IE8)) { return Browser.IE8; }
        if (distinguishAgent(requestUserAgent, IE7)) { return Browser.IE7; }
        if (distinguishAgent(requestUserAgent, IE6)) { return Browser.IE6; }
        if (distinguishAgent(requestUserAgent, MAX_THON)) { return Browser.MAX_THON; }
        if (distinguishAgent(requestUserAgent, QQ)) { return Browser.QQ; }
        if (distinguishAgent(requestUserAgent, GREEN)) { return Browser.GREEN; }
        if (distinguishAgent(requestUserAgent, SE360)) { return Browser.SE360; }
        if (distinguishAgent(requestUserAgent, FIRE_FOX)) { return Browser.FIRE_FOX; }
        if (distinguishAgent(requestUserAgent, OPERA)) { return Browser.OPERA; }
        if (distinguishAgent(requestUserAgent, CHROME)) { return Browser.CHROME; }
        if (distinguishAgent(requestUserAgent, SAFARI)) { return Browser.SAFARI; }
        if (distinguishAgent(requestUserAgent, CAMINO)) { return Browser.CAMINO; }
        if (distinguishAgent(requestUserAgent, GECKO)) { return Browser.GECKO; }

        return Browser.OTHER;
    }


    private static boolean distinguishAgent(String requestUserAgent, String agent) {
        Pattern p = Pattern.compile(agent, Pattern.MULTILINE);
        Matcher m = p.matcher(requestUserAgent);
        return m.find();
    }


    private final static String IE11 = "rv:11.0",
            IE10 = "MSIE 10.0",
            IE9 = "MSIE 9.0",
            IE8 = "MSIE 8.0",
            IE7 = "MSIE 7.0",
            IE6 = "MSIE 6.0",
            MAX_THON = "Maxthon",
            QQ = "QQBrowser",
            GREEN = "GreenBrowser",
            SE360 = "360SE",
            FIRE_FOX = "Firefox",
            OPERA = "Opera",
            CHROME = "Chrome",
            SAFARI = "Safari",
            CAMINO = "Camino",
            GECKO = "Gecko",
            OTHER = "其它";


    private enum Browser {

        IE6(""),
        IE7(""),
        IE8(""),
        IE9(""),
        IE10(""),
        IE11(""),
        FIRE_FOX("火狐浏览器"),
        MAX_THON("遨游浏览器"),
        QQ("QQ浏览器"),
        GREEN("GreenBrowser浏览器"),
        SE360("360安全浏览器"),
        OTHER("其它浏览器"),
        SAFARI("苹果Safari浏览器"),
        CHROME("谷歌浏览器"),
        OPERA("Opera浏览器"),
        CAMINO("MAC OS X下Camino浏览器"),
        GECKO("基于Gecko引擎开发的浏览器");

        private String describe;

        Browser(String describe) {
            this.describe = describe;
        }
    }


}
阅读更多

扫码向博主提问

简简单单OnlineZuozuo

欢迎提问,技术群172083832
  • 擅长领域:
  • JavaEE
  • java
  • 后台开发
  • 系统架构
  • 后端
去开通我的Chat快问
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_15071263/article/details/79963534
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭