创建Maven项目
添加pom.xml 依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xusan</groupId>
<artifactId>user-agent-053018</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>cz.mallat.uasparser</groupId>
<artifactId>uasparser</artifactId>
<version>0.6.1</version>
</dependency>
</dependencies>
</project>
获取浏览器版本:window.navigator.userAgent
window.navigator.userAgent
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
应用代码
package com.xusan;
import java.io.IOException;
import cz.mallat.uasparser.OnlineUpdater;
import cz.mallat.uasparser.UASparser;
import cz.mallat.uasparser.UserAgentInfo;
/**
* Created by XuWei on 2018/5/30.
*/
public class UserAgentUtil {
static UASparser uasParser = null;
// 初始化uasParser对象
static {
try {
uasParser = new UASparser(OnlineUpdater.getVendoredInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36";
System.out.println(str);
try {
UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(str);
System.out.println("操作系统名称:"+userAgentInfo.getOsFamily());//
System.out.println("操作系统:"+userAgentInfo.getOsName());//
System.out.println("浏览器名称:"+userAgentInfo.getUaFamily());//
System.out.println("浏览器版本:"+userAgentInfo.getBrowserVersionInfo());//
System.out.println("设备类型:"+userAgentInfo.getDeviceType());
System.out.println("浏览器:"+userAgentInfo.getUaName());
System.out.println("类型:"+userAgentInfo.getType());
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
操作系统名称:Windows
操作系统:Windows
浏览器名称:Chrome
浏览器版本:66.0.3359.181
设备类型:Personal computer
浏览器:Chrome 66.0.3359.181
类型:Browser