java區分登錄者的設備(手機仍是電腦)

此次項目須要去根據用戶的設備去跳轉頁面,
我把主頁定位在一個空白頁,讓這個空白頁自動提交表單,
在後臺action會調用工具類
工具類的代碼:css

package com.util;

import javax.servlet.http.HttpServletRequest;

public class GetHttp {

    /** Wap網關Via頭信息中特有的描述信息 */
    private static String mobileGateWayHeaders[] = new String[] { "ZXWAP",// 中興提供的wap網關的via信息,例如:Via=ZXWAP
                                                                            // GateWayZTE
                                                                            // Technologies,
            "chinamobile.com",// 中國移動的諾基亞wap網關,例如:Via=WTP/1.1
                                // GDSZ-PB-GW003-WAP07.gd.chinamobile.com (Nokia
                                // WAP Gateway 4.1 CD1/ECD13_D/4.1.04)
            "monternet.com",// 移動夢網的網關,例如:Via=WTP/1.1
                            // BJBJ-PS-WAP1-GW08.bj1.monternet.com. (Nokia WAP
                            // Gateway 4.1 CD1/ECD13_E/4.1.05)
            "infoX",// 華爲提供的wap網關,例如:Via=HTTP/1.1 GDGZ-PS-GW011-WAP2 (infoX-WISG
                    // Huawei Technologies),或Via=infoX WAP Gateway V300R001
                    // Huawei Technologies
            "XMS 724Solutions HTG",// 國外電信運營商的wap網關,不知道是哪一家
            "wap.lizongbo.com",// 本身測試時模擬的頭信息
            "Bytemobile",// 貌似是一個給移動互聯網提供解決方案提升網絡運行效率的,例如:Via=1.1 Bytemobile OSN
                            // WebProxy/5.1
    };
    /** 電腦上的IE或Firefox瀏覽器等的User-Agent關鍵詞 */
    private static String[] pcHeaders = new String[] { "Windows 98",
            "Windows ME", "Windows 2000", "Windows XP", "Windows NT", "Ubuntu" };
    /** 手機瀏覽器的User-Agent裏的關鍵詞 */
    private static String[] mobileUserAgents = new String[] { "Nokia",// 諾基亞,有山寨機也寫這個的,總還算是手機,Mozilla/5.0
                                                                        // (Nokia5800
                                                                        // XpressMusic)UC
                                                                        // AppleWebkit(like
                                                                        // Gecko)
                                                                        // Safari/530
            "SAMSUNG",// 三星手機
                        // SAMSUNG-GT-B7722/1.0+SHP/VPP/R5+Dolfin/1.5+Nextreaming+SMM-MMS/1.2.0+profile/MIDP-2.1+configuration/CLDC-1.1
            "MIDP-2",// j2me2.0,Mozilla/5.0 (SymbianOS/9.3; U; Series60/3.2
                        // NokiaE75-1 /110.48.125 Profile/MIDP-2.1
                        // Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML like
                        // Gecko) Safari/413
            "CLDC1.1",// M600/MIDP2.0/CLDC1.1/Screen-240X320
            "SymbianOS",// 塞班系統的,
            "MAUI",// MTK山寨機默認ua
            "UNTRUSTED/1.0",// 疑似山寨機的ua,基本能夠肯定仍是手機
            "Windows CE",// Windows CE,Mozilla/4.0 (compatible; MSIE 6.0;
                            // Windows CE; IEMobile 7.11)
            "iPhone",// iPhone是否也轉wap?無論它,先區分出來再說。Mozilla/5.0 (iPhone; U; CPU
                        // iPhone OS 4_1 like Mac OS X; zh-cn) AppleWebKit/532.9
                        // (KHTML like Gecko) Mobile/8B117
            "iPad",// iPad的ua,Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X;
                    // zh-cn) AppleWebKit/531.21.10 (KHTML like Gecko)
                    // Version/4.0.4 Mobile/7B367 Safari/531.21.10
            "Android",// Android是否也轉wap?Mozilla/5.0 (Linux; U; Android
                        // 2.1-update1; zh-cn; XT800 Build/TITA_M2_16.22.7)
                        // AppleWebKit/530.17 (KHTML like Gecko) Version/4.0
                        // Mobile Safari/530.17
            "BlackBerry",// BlackBerry8310/2.7.0.106-4.5.0.182
            "UCWEB",// ucweb是否只給wap頁面? Nokia5800
                    // XpressMusic/UCWEB7.5.0.66/50/999
            "ucweb",// 小寫的ucweb貌似是uc的代理服務器Mozilla/6.0 (compatible; MSIE 6.0;)
                    // Opera ucweb-squid
            "BREW",// 很奇怪的ua,例如:REW-Applet/0x20068888 (BREW/3.1.5.20; DeviceId:
                    // 40105; Lang: zhcn) ucweb-squid
            "J2ME",// 很奇怪的ua,只有J2ME四個字母
            "YULONG",// 宇龍手機,YULONG-CoolpadN68/10.14 IPANEL/2.0 CTC/1.0
            "YuLong",// 仍是宇龍
            "COOLPAD",// 宇龍酷派YL-COOLPADS100/08.10.S100 POLARIS/2.9 CTC/1.0
            "TIANYU",// 天語手機TIANYU-KTOUCH/V209/MIDP2.0/CLDC1.1/Screen-240X320
            "TY-",// 天語,TY-F6229/701116_6215_V0230 JUPITOR/2.2 CTC/1.0
            "K-Touch",// 仍是天語K-Touch_N2200_CMCC/TBG110022_1223_V0801 MTK/6223
                        // Release/30.07.2008 Browser/WAP2.0
            "Haier",// 海爾手機,Haier-HG-M217_CMCC/3.0 Release/12.1.2007
                    // Browser/WAP2.0
            "DOPOD",// 多普達手機
            "Lenovo",// 聯想手機,Lenovo-P650WG/S100 LMP/LML Release/2010.02.22
                        // Profile/MIDP2.0 Configuration/CLDC1.1
            "LENOVO",// 聯想手機,好比:LENOVO-P780/176A
            "HUAQIN",// 華勤手機
            "AIGO-",// 愛國者竟然也出過手機,AIGO-800C/2.04 TMSS-BROWSER/1.0.0 CTC/1.0
            "CTC/1.0",// 含義不明
            "CTC/2.0",// 含義不明
            "CMCC",// 移動定製手機,K-Touch_N2200_CMCC/TBG110022_1223_V0801 MTK/6223
                    // Release/30.07.2008 Browser/WAP2.0
            "DAXIAN",// 大顯手機DAXIAN X180 UP.Browser/6.2.3.2(GUI) MMP/2.0
            "MOT-",// 摩托羅拉,MOT-MOTOROKRE6/1.0 LinuxOS/2.4.20 Release/8.4.2006
                    // Browser/Opera8.00 Profile/MIDP2.0 Configuration/CLDC1.1
                    // Software/R533_G_11.10.54R
            "SonyEricsson",// 索愛手機,SonyEricssonP990i/R100 Mozilla/4.0
                            // (compatible; MSIE 6.0; Symbian OS; 405) Opera
                            // 8.65 [zh-CN]
            "GIONEE",// 金立手機
            "HTC",// HTC手機
            "ZTE",// 中興手機,ZTE-A211/P109A2V1.0.0/WAP2.0 Profile
            "HUAWEI",// 華爲手機,
            "webOS",// palm手機,Mozilla/5.0 (webOS/1.4.5; U; zh-CN)
                    // AppleWebKit/532.2 (KHTML like Gecko) Version/1.0
                    // Safari/532.2 Pre/1.0
            "GoBrowser",// 3g GoBrowser.User-Agent=Nokia5230/GoBrowser/2.0.290
                        // Safari
            "IEMobile",// Windows CE手機自帶瀏覽器,
            "WAP2.0"// 支持wap 2.0的
    };

    /**
     * 根據當前請求的特徵,判斷該請求是否來自手機終端,主要檢測特殊的頭信息,以及user-Agent這個header
     * 
     * @param request
     *            http請求
     * @return 若是命中手機特徵規則,則返回對應的特徵字符串
     */
    public static boolean isMobileDevice(HttpServletRequest request) {
        boolean b = false;
        boolean pcFlag = false;
        boolean mobileFlag = false;
        String via = request.getHeader("Via");
        System.out.println(via);
        String userAgent = request.getHeader("user-agent");
        System.out.println(userAgent);
        for (int i = 0; via != null && !via.trim().equals("")
                && i < mobileGateWayHeaders.length; i++) {
            if (via.contains(mobileGateWayHeaders[i])) {
                mobileFlag = true;
                break;
            }
        }
        for (int i = 0; !mobileFlag && userAgent != null
                && !userAgent.trim().equals("") && i < mobileUserAgents.length; i++) {
            if (userAgent.contains(mobileUserAgents[i])) {
                mobileFlag = true;
                break;
            }
        }
        for (int i = 0; userAgent != null && !userAgent.trim().equals("")
                && i < pcHeaders.length; i++) {
            if (userAgent.contains(pcHeaders[i])) {
                pcFlag = true;
                break;
            }
        }
        if (mobileFlag == true && pcFlag == false) {
            b = true;
        }
        return b;// false pc true shouji

    }
}

而在action調用中,我是這樣作的:java

HttpServletRequest request = ServletActionContext.getRequest();
        Boolean b = GetHttp.isMobileDevice(request);
        System.out.println("This device type is: " + b
                + "  (true is means telephone, other means pc or pad)");
        if (b.equals(true)) {
            return "mobisucc";
        } else
            return "pcsucc";
    }
相關文章
相關標籤/搜索