<browscap.version>1.3.12</browscap.version>
<!-- 解析客户端操作系统、浏览器等 -->
<dependency>
<groupId>com.blueconic</groupId>
<artifactId>browscap-java</artifactId>
<version>${browscap.version}</version>
</dependency>
<dependency>
<groupId>com.blueconic</groupId>
<artifactId>browscap-java</artifactId>
</dependency>
package com.ruoyi.common.utils.http;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.blueconic.browscap.Capabilities;
import com.blueconic.browscap.UserAgentParser;
import com.blueconic.browscap.UserAgentService;
import com.ruoyi.common.utils.AddressUtils;
/**
* 浏览器用户代理解析
*
*/
public class UserAgent
{
private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
/** 浏览器 */
public String browser = "";
/** 操作系统 */
public String operatingSystem = "";
/** 解析器 */
private static UserAgentParser parser = null;
static
{
try
{
parser = new UserAgentService().loadParser();
}
catch (Exception e)
{
log.error("获取用户代理异常 {}", e);
}
}
public UserAgent(String userAgentString)
{
if (parser != null)
{
String userAgentLowercaseString = userAgentString == null ? null : userAgentString.toLowerCase();
Capabilities capabilities = parser.parse(userAgentLowercaseString);
this.browser = String.format("%s %s", capabilities.getBrowser(), capabilities.getBrowserMajorVersion());
this.operatingSystem = capabilities.getPlatform();
}
}
public static UserAgent parseUserAgentString(String userAgentString)
{
return new UserAgent(userAgentString);
}
public String getBrowser()
{
return browser;
}
public String getOperatingSystem()
{
return operatingSystem;
}
}
// bitwalker 获取浏览器/操作系统方法
String os = userAgent.getOperatingSystem().getName();
String browser = userAgent.getBrowser().getName();
// ======== 修改为 ========
// browscap-java 获取浏览器/操作系统方法
String os = userAgent.getOperatingSystem();
String browser = userAgent.getBrowser();
powered by kaifamiao