標籤:java
package com.hanchao.util;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * @author liweihan () * @version 1.0 (2014年11月28日 下午2:31:56) * 檢查是否是移動端的訪問 */public 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; } } }
參考文章:http://www.cnblogs.com/rubylouvre/archive/2012/04/11/2442588.html
http://blog.csdn.net/xiaoxian8023/article/details/37527133
本文出自 “我的JAVA世界” 部落格,請務必保留此出處http://hanchaohan.blog.51cto.com/2996417/1584251
java-判斷是移動端還是PC端訪問