java-判斷是移動端還是PC端訪問

來源:互聯網
上載者:User

標籤: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端訪問

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.