UserAgent 정보 도구 클래스 가 져 오기
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cz.mallat.uasparser.OnlineUpdater;
import cz.mallat.uasparser.UASparser;
import cz.mallat.uasparser.UserAgentInfo;
/**
* UserAgent ,jdk1.7
*/
public class UserAgentUtil {
private static final Logger LOG = LoggerFactory.getLogger(UserAgentUtil.class);
private static UASparser uasParser = null;
public static String android="Android";
public static String iphone="iPhone";
public static String ipad="iPad";
public static final String noDevice=" ";
// \b ( ( ) ),
// , "\\b"
// \B ( )
private static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i"
+"|windows (phone|ce)|blackberry"
+"|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"
+"|laystation portable)|nokia|fennec|htc[-_]"
+"|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
private static String tableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser"
+"|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
// : 、
private static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE);
private static Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE);
static {
try {
uasParser = new UASparser(OnlineUpdater.getVendoredInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
*
* @Title: check
* @param userAgent
* @return true: ,false:pc
*/
public static boolean checkMobileOrPC(HttpServletRequest request){
String userAgent=request.getHeader("user-agent").toLowerCase();
System.out.println("========================== ========================================");
System.out.println(" :"+userAgent);
//
Matcher matcherPhone = phonePat.matcher(userAgent);
Matcher matcherTable = tablePat.matcher(userAgent);
boolean result=false;
if(matcherPhone.find() || matcherTable.find()){
result = true;
}
System.out.println("=========================== =======================================");
return result;
}
/**
*
*
* @Title: check
* @param userAgent
* @return true: ,false:pc
*/
public static boolean checkMobileOrPC(String userAgent){
System.out.println(" :"+userAgent);
//
Matcher matcherPhone = phonePat.matcher(userAgent);
Matcher matcherTable = tablePat.matcher(userAgent);
if(matcherPhone.find() || matcherTable.find()){
return true;
} else {
return false;
}
}
/**
* Smartphone/Personal computer
* @param userAgent
* @return
* @throws IOException
*/
public static String getDeviceType(HttpServletRequest request) throws IOException{
String userAgent=request.getHeader("user-agent").toLowerCase();
UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
LOG.debug(" :" + userAgentInfo.getDeviceType());
return userAgentInfo.getDeviceType();
}
/**
* Android/Windows/iOS
* @param userAgent
* @return
* @throws IOException
*/
public static String getOsName(HttpServletRequest request) throws IOException{
String userAgent=request.getHeader("user-agent").toLowerCase();
UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
LOG.debug(" :" + userAgentInfo.getOsName());
return userAgentInfo.getOsName();
}
/**
* Chrome Mobile 53.0.2785.146/Chrome 63.0.3239.84/Android browser 4.0
* @param userAgent
* @return
* @throws IOException
*/
public static String getUaName(HttpServletRequest request) throws IOException{
String userAgent=request.getHeader("user-agent").toLowerCase();
UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
LOG.debug(" :" + userAgentInfo.getUaName());
return userAgentInfo.getUaName();
}
/**
*
* @param request
* @return
*/
public static boolean isWeChatBrowser(HttpServletRequest request){
String userAgent=request.getHeader("user-agent").toLowerCase();
return userAgent.toLowerCase().indexOf("micromessenger")>-1?true:false;
}
/**
* ios ipad
* @param request
* @return
*/
public static String getMobileModel(HttpServletRequest request){
String userAgent=request.getHeader("user-agent").toLowerCase();
// **************************************************************************
// Android
Pattern pattern = Pattern.compile(";\\s?(\\S*?\\s?\\S*?)\\s?(Build)?/");
Matcher matcher = pattern.matcher(userAgent);
String mobileModel = null;
if (matcher.find()) {
mobileModel = matcher.group(1).trim();
}
// iphone ipad
if (userAgent.indexOf("iphone") > 0) {
mobileModel = "iphone";
}
if (userAgent.indexOf("ipad") > 0) {
mobileModel = "ipad";
}
// **************************************************************************
return mobileModel;
}
/**
* Mobile Browser/Browser
* @param userAgent
* @return
* @throws IOException
*/
public static String getBrowerType(HttpServletRequest request) throws IOException{
String userAgent=request.getHeader("user-agent").toLowerCase();
UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
LOG.debug(" :" + userAgentInfo.getType());
return userAgentInfo.getType();
}
/**
* Chrome Mobile 53.0.2785.146/Chrome 63.0.3239.84
* @param userAgent
* @return
* @throws IOException
*/
public static String getBrowserVersionInfo(HttpServletRequest request) throws IOException{
String userAgent=request.getHeader("user-agent").toLowerCase();
UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
LOG.debug(" :" + userAgentInfo.getBrowserVersionInfo());
return userAgentInfo.getBrowserVersionInfo();
}
/**
*
* @param userAgent
* @return
*/
public static String getMobileOS(HttpServletRequest request){
String userAgent=request.getHeader("user-agent").toLowerCase();
if (userAgent.contains(android)) {
return android;
}else if (userAgent.contains(iphone)){
return iphone;
}else if (userAgent.contains(ipad)){
return ipad;
}else {
return "others";
}
}
/**
*
* @param userAgent
* @return
* @throws IOException
*/
public static String getOsFamily(HttpServletRequest request) throws IOException{
String userAgent=request.getHeader("user-agent").toLowerCase();
UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
LOG.debug(" :" + userAgentInfo.getOsFamily());
return userAgentInfo.getOsFamily();
}
/**
*
* @param userAgent
* @return
* @throws IOException
*/
public static String getUaFamily(HttpServletRequest request) throws IOException{
String userAgent=request.getHeader("user-agent").toLowerCase();
UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(userAgent);
LOG.debug(" :" + userAgentInfo.getUaFamily());
return userAgentInfo.getUaFamily();
}
}
필요 maven:
cz.mallat.uasparser
uasparser
0.6.2
net.sourceforge.jregex
jregex
1.2_01
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[MeU] Hashtag 기능 개발➡️ 기존 Tag 테이블에 존재하지 않는 해시태그라면 Tag , tagPostMapping 테이블에 모두 추가 ➡️ 기존에 존재하는 해시태그라면, tagPostMapping 테이블에만 추가 이후에 개발할 태그 기반 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.