获取用户浏览器及操作系统信息

在pom.xml中引入依赖jar

        <dependency>
            <groupId>eu.bitwalker</groupId>
            <artifactId>UserAgentUtils</artifactId>
            <version>1.20</version>
        </dependency>
public static String getBrowser(HttpServletRequest request) {
        String header = request.getHeader("User-Agent");
        System.out.println(header);
        UserAgent userAgent = UserAgent.parseUserAgentString(header);
        // 获取客户端操作系统
        OperatingSystem operatingSystem = userAgent.getOperatingSystem();
        // 获取客户端浏览器
        Browser browser = userAgent.getBrowser();
        String browserName = browser.getName();
        String sysName = operatingSystem.getName();
        if ("Chrome".equals(browser.getName())) {
            if (header.indexOf("QQBrowser") > 0) {
                browserName = "QQBrowser";
            }
        }
        System.out.println("浏览器名:" + browserName);
        System.out.println("浏览器类型:" + browser.getBrowserType());
        System.out.println("浏览器家族:" + browser.getGroup());
        System.out.println("浏览器生产厂商:" + browser.getManufacturer());
        System.out.println("浏览器使用的渲染引擎:" + browser.getRenderingEngine());
        System.out.println("浏览器版本:" + userAgent.getBrowserVersion());
        System.out.println("\n操作系统名:" + operatingSystem.getName());
        System.out.println("访问设备类型:" + operatingSystem.getDeviceType());
        System.out.println("操作系统家族:" + operatingSystem.getGroup());
        System.out.println("操作系统生产厂商:" + operatingSystem.getManufacturer());
        String result = sysName + "上的" + browserName;
        System.out.println(result);
        return result;
    }
posted @ 2019-03-29 15:47  知识追求者  阅读(2172)  评论(0编辑  收藏  举报