判断终端类型

public static class CheckMobile {  

// \b 是单词边界(连着的两个(字母字符 与 非字母字符) 之间的逻辑上的间隔),
// 字符串在编译时会被转码一次,所以是 "\\b"
// \B 是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔)
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";
static String tableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser"
+"|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";

//移动设备正则匹配:手机端、平板
static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE);
static Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE);

/**
* 检测是否是移动设备访问
*
* @Title: check
* @Date : 2014-7-7 下午01:29:07
* @param userAgent 浏览器标识
* @return true:移动设备接入,false:pc端接入
*/
public static boolean check(String userAgent){
if(null == userAgent){
userAgent = "";
}
// 匹配
Matcher matcherPhone = phonePat.matcher(userAgent);
Matcher matcherTable = tablePat.matcher(userAgent);
if(matcherPhone.find() || matcherTable.find()){
return true;
} else {
return false;
}
}
}

/**
* 检查访问方式是否为移动端
*
* @Title: check
* @Date : 2014-7-7 下午03:55:19
* @param request
* @throws IOException
*/
public boolean check(HttpServletRequest request) throws IOException{
boolean isFromMobile=false;

HttpSession session= request.getSession();
//检查是否已经记录访问方式(移动端或pc端)
if(null==session.getAttribute("ua")){
try{
//获取ua,用来判断是否为移动端访问
String userAgent = request.getHeader( "USER-AGENT" ).toLowerCase();
if(null == userAgent){
userAgent = "";
}
isFromMobile=CheckMobile.check(userAgent);
//判断是否为移动端访问
if(isFromMobile){
logger.info("移动端访问");
session.setAttribute("ua","mobile");
} else {
logger.info("pc端访问");
session.setAttribute("ua","pc");
}
}catch(Exception e){}
}else{
isFromMobile=session.getAttribute("ua").equals("mobile");
}

return isFromMobile;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值