根據 User Agent String 判斷客戶端平臺及瀏覽器的 Java 程式
阿新 • • 發佈:2019-02-05
現在很多網站都支援判斷客戶端平臺、瀏覽器,比如新浪微博、http://zww.me/。個人總結了一下根據 User Agent String 判斷客戶端的 Java 程式,未完待續,作者有空的話會隨時更新,目前只支援主流的幾個平臺和瀏覽器。也希望朋友們可以進行補充,如果有什麼好的意見可以跟帖留言,畢竟個人的力量是有限的,謝謝。
以上 Java 版和 PHP 版的根據 User Agent String 判斷客戶端的源程式都已上傳 Csdn 資源,有興趣的朋友可以去下載下來看看。下載地址:根據User Agent String判斷客戶端平臺及瀏覽器源程式[Java和PHP版]。
資料封裝的 UserAgent 程式碼如下:
package com.defonds.sample.web.entity;/** * 根據 user agent string 來判斷出客戶端的瀏覽器以及平臺等資訊 * @author Defonds */public class UserAgent { private String browserType;//瀏覽器型別 private String browserVersion;//瀏覽器版本 private String platformType;//平臺型別 private String platformSeries;//平臺系列 private String platformVersion;//平臺版本 public UserAgent(){} public UserAgent(String browserType, String browserVersion, String platformType, String platformSeries, String platformVersion){ this.browserType = browserType; this.browserVersion = browserVersion; this.platformType = platformType; this.platformSeries = platformSeries; this .platformVersion = platformVersion; } public String getBrowserType() { return browserType; } public void setBrowserType(String browserType) { this.browserType = browserType; } public String getBrowserVersion() { return browserVersion; } public void setBrowserVersion(String browserVersion) { this .browserVersion = browserVersion; } public String getPlatformType() { return platformType; } public void setPlatformType(String platformType) { this.platformType = platformType; } public String getPlatformSeries() { return platformSeries; } public void setPlatformSeries(String platformSeries) { this.platformSeries = platformSeries; } public String getPlatformVersion() { return platformVersion; } public void setPlatformVersion(String platformVersion) { this.platformVersion = platformVersion; } }
判斷處理的 UserAgentUtil 程式碼如下:
package com.defonds.sample.common.util;import org.apache.commons.lang.StringUtils;import com.defonds.sample.web.entity.UserAgent;/** * 根據 user agent string 判斷使用者的平臺、瀏覽器 * 參考資料 * ************************************************************************************************************************************************** * * 桌上型電腦 * * Linux Ubuntu * Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.2pre) Gecko/20100225 Ubuntu/9.10 (karmic) Namoroka/3.6.2pre * -------------------------------------------------------------------------------------------------------------------------------------------------- * Linux Mandriva 2008.1 * Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008072403 Mandriva/3.0.1-1mdv2008.1 (2008.1) Firefox/3.0.1 * -------------------------------------------------------------------------------------------------------------------------------------------------- * Linux suSE 10.1 * Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.3) Gecko/20060425 SUSE/1.5.0.3-7 Firefox/1.5.0.31 * -------------------------------------------------------------------------------------------------------------------------------------------------- * Windows XP SP3 * Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR 3.5.30729) * -------------------------------------------------------------------------------------------------------------------------------------------------- * Windows Vista * Mozilla/5.0 (Windows; U; Windows NT 6.1; nl; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 * Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729) * -------------------------------------------------------------------------------------------------------------------------------------------------- * windows 2000 * Mozilla/5.0 (Windows; U; Windows NT 5.0; en-GB; rv:1.8.1b2) Gecko/20060821 Firefox/2.0b2 * -------------------------------------------------------------------------------------------------------------------------------------------------- * Windows 7 * Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1 * -------------------------------------------------------------------------------------------------------------------------------------------------- * Windows Server 2008 * Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729) * -------------------------------------------------------------------------------------------------------------------------------------------------- * iMac OSX 10.7.4 * Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0.1 * -------------------------------------------------------------------------------------------------------------------------------------------------- * Mac OS X * Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9 * -------------------------------------------------------------------------------------------------------------------------------------------------- * * 手持裝置 * * iPad * Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10 * -------------------------------------------------------------------------------------------------------------------------------------------------- * iPad 2 * Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X; en-us) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3 * -------------------------------------------------------------------------------------------------------------------------------------------------- * iPhone 4 * Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7 * -------------------------------------------------------------------------------------------------------------------------------------------------- * iPhone 5 * Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3 * -------------------------------------------------------------------------------------------------------------------------------------------------- * Android * Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 * ************************************************************************************************************************************************** * @author Defonds */public class UserAgentUtil { /** * 用途:根據客戶端 User Agent Strings 判斷其瀏覽器、操作平臺 * if 判斷的先後次序: * 根據裝置的使用者使用量降序排列,這樣對於大多數使用者來說可以少判斷幾次即可拿到結果: * >>作業系統:Windows > 蘋果 > 安卓 > Linux > ... * >>Browser:Chrome > FF > IE > ... * @param userAgentStr * @return */ public static UserAgent getUserAgent(String userAgent) { if (StringUtils.isBlank(userAgent)) { return null; } if (userAgent.contains("Windows")) {//主流應用靠前 /** * ****************** * 桌上型電腦 Windows 系列 * ****************** * Windows NT 6.2 - Windows 8 * Windows NT 6.1 - Windows 7 * Windows NT 6.0 - Windows Vista * Windows NT 5.2 - Windows Server 2003; Windows XP x64 Edition * Windows NT 5.1 - Windows XP * Windows NT 5.01 - Windows 2000, Service Pack 1 (SP1) * Windows NT 5.0 - Windows 2000 * Windows NT 4.0 - Microsoft Windows NT 4.0 * Windows 98; Win 9x 4.90 - Windows Millennium Edition (Windows Me) * Windows 98 - Windows 98 * Windows 95 - Windows 95 * Windows CE - Windows CE * 判斷依據:http://msdn.microsoft.com/en-us/library/ms537503(v=vs.85).aspx */ if (userAgent.contains("Windows NT 6.2")) {//Windows 8 return judgeBrowser(userAgent, "Windows", "8" , null);//判斷瀏覽器 } else if (userAgent.contains("Windows NT 6.1")) {//Windows 7 return judgeBrowser(userAgent, "Windows", "7" , null); } else if (userAgent.contains("Windows NT 6.0")) {//Windows Vista return judgeBrowser(userAgent, "Windows", "Vista" , null); } else if (userAgent.contains("Windows NT 5.2")) {//Windows XP x64 Edition return judgeBrowser(userAgent, "Windows", "XP" , "x64 Edition"); } else if (userAgent.contains("Windows NT 5.1")) {//Windows XP return judgeBrowser(userAgent, "Windows", "XP" , null); } else if (userAgent.contains("Windows NT 5.01")) {//Windows 2000, Service Pack 1 (SP1) return judgeBrowser(userAgent, "Windows", "2000" , "SP1"); } else if (userAgent.contains("Windows NT 5.0")) {//Windows 2000 return judgeBrowser(userAgent, "Windows", "2000" , null); } else if (userAgent.contains("Windows NT 4.0")) {//Microsoft Windows NT 4.0 return judgeBrowser(userAgent, "Windows", "NT 4.0" , null); } else if (userAgent.contains("Windows 98; Win 9x 4.90")) {//Windows Millennium Edition (Windows Me) return judgeBrowser(userAgent, "Windows", "ME" , null); } else if (userAgent.contains("Windows 98")) {//Windows 98 return judgeBrowser(userAgent, "Windows", "98" , null); } else if (userAgent.contains("Windows 95")) {//Windows 95 return judgeBrowser(userAgent, "Windows", "95" , null); } else if (userAgent.contains("Windows CE")) {//Windows CE return judgeBrowser(userAgent, "Windows", "CE" , null); } } else if (userAgent.contains("Mac OS X")) { /** * ******** * 蘋果系列 * ******** * iPod - Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5 * iPad - Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10 * iPad2 - Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X; en-us) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3 * iPhone 4 - Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7 * iPhone 5 - Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3 * 判斷依據:http://www.useragentstring.com/pages/Safari/ * 參考:http://stackoverflow.com/questions/7825873/what-is-the-ios-5-0-user-agent-string * 參考:http://stackoverflow.com/questions/3105555/what-is-the-iphone-4-user-agent */ if (userAgent.contains("iPod")) { return judgeBrowser(userAgent, "iPod", null , null);//判斷瀏覽器 } } return null; } /** * 用途:根據客戶端 User Agent Strings 判斷其瀏覽器 * if 判斷的先後次序: * 根據瀏覽器的使用者使用量降序排列,這樣對於大多數使用者來說可以少判斷幾次即可拿到結果: * >>Browser:Chrome > FF > IE > ... * @param userAgent:user agent * @param platformType:平臺 * @param platformSeries:系列 * @param platformVersion:版本 * @return */ private static UserAgent judgeBrowser(String userAgent, String platformType, String platformSeries, String platformVersion) { if (userAgent.contains("Chrome")) { /** * *********** * Chrome 系列 * *********** * Chrome 24.0.1295.0 - Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15 * Chrome 24.0.1292.0 - Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.14 (KHTML, like Gecko) Chrome/24.0.1292.0 Safari/537.14 * Chrome 24.0.1290.1 - Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.13 (KHTML, like Gecko) Chrome/24.0.1290.1 Safari/537.13 * 判斷依據:http://www.useragentstring.com/pages/Chrome/ */ String temp = userAgent.substring(userAgent.indexOf("Chrome/") + 7);//拿到User Agent String "Chrome/" 之後的字串,結果形如"24.0.1295.0 Safari/537.15"或"24.0.1295.0" String chromeVersion = null; if (temp.indexOf(" ") < 0) {//temp形如"24.0.1295.0" chromeVersion = temp; } else {//temp形如"24.0.1295.0 Safari/537.15" chromeVersion = temp.substring(0, temp.indexOf(" ")); } return new UserAgent("Chrome", chromeVersion, platformType, platformSeries, platformVersion); } else if (userAgent.contains("Firefox")) { /** * ******* * FF 系列 * ******* * Firefox 16.0.1 - Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1 * Firefox 15.0a2 - Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2 * Firefox 15.0.2 - Mozilla/5.0 (Windows NT 6.2; WOW64; rv:15.0) Gecko/20120910144328 Firefox/15.0.2 * 判斷依據:http://www.useragentstring.com/pages/Firefox/ */ String temp = userAgent.substring(userAgent.indexOf("Firefox/") + 8);//拿到User Agent String "Firefox/" 之後的字串,結果形如"16.0.1 Gecko/20121011"或"16.0.1" String ffVersion = null; if (temp.indexOf(" ") < 0) {//temp形如"16.0.1" ffVersion = temp; } else {//temp形如"16.0.1 Gecko/20121011" ffVersion = temp.substring(0, temp.indexOf(" ")); } return new UserAgent("Firefox", ffVersion, platformType, platformSeries, platformVersion); } else if (userAgent.contains("MSIE")) { /** * ******* * IE 系列 * ******* * MSIE 10.0 - Internet Explorer 10 * MSIE 9.0 - Internet Explorer 9 * MSIE 8.0 - Internet Explorer 8 or IE8 Compatibility View/Browser Mode * MSIE 7.0 - Windows Internet Explorer 7 or IE7 Compatibility View/Browser Mode * MSIE 6.0 - Microsoft Internet Explorer 6 * 判斷依據:http://msdn.microsoft.com/en-us/library/ms537503(v=vs.85).aspx */ if (userAgent.contains("MSIE 10.0")) {//Internet Explorer 10 return new UserAgent("Internet Explorer", "10", platformType, platformSeries, platformVersion); } else if (userAgent.contains("MSIE 9.0")) {//Internet Explorer 9 return new UserAgent("Internet Explorer", "9", platformType, platformSeries, platformVersion); } else if (userAgent.contains("MSIE 8.0")) {//Internet Explorer 8 return new UserAgent("Internet Explorer", "8", platformType, platformSeries, platformVersion); } else if (userAgent.contains("MSIE 7.0")) {//Internet Explorer 7 return new UserAgent("Internet Explorer", "7", platformType, platformSeries, platformVersion); } else if (userAgent.contains("MSIE 6.0")) {//Internet Explorer 6 return new UserAgent("Internet Explorer", "6", platformType, platformSeries, platformVersion); } } else {//暫時支援以上三個主流.其它瀏覽器,待續... return new UserAgent(null, null, platformType, platformSeries, platformVersion); } return null; }}
後記,同事分享了一個 PHP 版的 User Agent String 判斷客戶端的原始碼,支援的比較全,大家也可以參考一下。部分程式碼摘錄如下:
function CID_windows_detect_os($ua) { $os_name = $os_code = $os_ver = $pda_name = $pda_code = $pda_ver = null; if (preg_match('/Windows 95/i', $ua) || preg_match('/Win95/', $ua)) { $os_name = "Windows"; $os_code = "windows"; $os_ver = "95"; } elseif (preg_match('/Windows NT 5.0/i', $ua) || preg_match('/Windows 2000/i', $ua)) { $os_name = "Windows"; $os_code = "windows"; $os_ver = "2000"; } elseif (preg_match('/Win 9x 4.90/i', $ua) || preg_match('/Windows ME/i', $ua)) { $os_name = "Windows"; $os_code = "windows"; $os_ver = "ME"; } elseif (preg_match('/Windows.98/i', $ua) || preg_match('/Win98/i', $ua)) { $os_name = "Windows"; $os_code = "windows"; $os_ver = "98"; } elseif (preg_match('/Windows NT 6.0/i', $ua)) { $os_name = "Windows"; $os_code = "windows_vista"; $os_ver = "Vista"; } elseif (preg_match('/Windows NT 6.1/i', $ua)) { $os_name = "Windows"; $os_code = "windows_win7"; $os_ver = "7"; } elseif (preg_match('/Windows NT 5.1/i', $ua)) { $os_name = "Windows"; $os_code = "windows"; $os_ver = "XP"; } elseif (preg_match('/Windows NT 5.2/i', $ua)) { $os_name = "Windows"; $os_code = "windows"; if (preg_match('/Win64/i', $ua)) { $os_ver = "XP 64 bit"; } else { $os_ver = "Server 2003"; } } elseif (preg_match('/Mac_PowerPC/i', $ua)) { $os_name = "Mac OS"; $os_code = "macos"; }elseif (preg_match('/Windows Phone/i', $ua)) { $matches = explode(';',$ua); $os_name = $matches[2]; $os_code = "windows_phone7"; } elseif (preg_match('/Windows NT 4.0/i', $ua) || preg_match('/WinNT4.0/i', $ua)) { $os_name = "Windows"; $os_code = "windows"; $os_ver = "NT 4.0"; } elseif (preg_match('/Windows NT/i', $ua) || preg_match('/WinNT/i', $ua)) { $os_name = "Windows"; $os_code = "windows"; $os_ver = "NT";
以上 Java 版和 PHP 版的根據 User Agent String 判斷客戶端的源程式都已上傳 Csdn 資源,有興趣的朋友可以去下載下來看看。下載地址:根據User Agent String判斷客戶端平臺及瀏覽器源程式[Java和PHP版]。