public
class
BrowseTool {
07 | private final static String IE9= "MSIE 9.0" ; |
08 | private final static String IE8= "MSIE 8.0" ; |
09 | private final static String IE7= "MSIE 7.0" ; |
10 | private final static String IE6= "MSIE 6.0" ; |
11 | private final static String MAXTHON= "Maxthon" ; |
12 | private final static String QQ= "QQBrowser" ; |
13 | private final static String GREEN= "GreenBrowser" ; |
14 | private final static String SE360= "360SE" ; |
15 | private final static String FIREFOX= "Firefox" ; |
16 | private final static String OPERA= "Opera" ; |
17 | private final static String CHROME= "Chrome" ; |
18 | private final static String SAFARI= "Safari" ; |
19 | private final static String OTHER= "其它" ; |
22 | public String checkBrowse(String userAgent){ |
23 | if (regex(OPERA, userAgent)) return OPERA; |
24 | if (regex(CHROME, userAgent)) return CHROME; |
25 | if (regex(FIREFOX, userAgent)) return FIREFOX; |
26 | if (regex(SAFARI, userAgent)) return SAFARI; |
27 | if (regex(SE360, userAgent)) return SE360; |
28 | if (regex(GREEN,userAgent)) return GREEN; |
29 | if (regex(QQ,userAgent)) return QQ; |
30 | if (regex(MAXTHON, userAgent)) return MAXTHON; |
31 | if (regex(IE9,userAgent)) return IE9; |
32 | if (regex(IE8,userAgent)) return IE8; |
33 | if (regex(IE7,userAgent)) return IE7; |
34 | if (regex(IE6,userAgent)) return IE6; |
37 | public boolean regex(String regex,String str){ |
38 | Pattern p =Pattern.compile(regex,Pattern.MULTILINE); |
39 | Matcher m=p.matcher(str); |
43 | public static void main(String[] args) { |
44 | String ie9 = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)" ; |
45 | String ie8 = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322)" ; |
46 | String ie7 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322)" ; |
47 | String ie6 = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)" ; |
48 | String aoyou = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; Maxthon 2.0)" ; |
49 | String qq = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322) QQBrowser/6.8.10793.201" ; |
50 | String green = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; GreenBrowser)" ; |
51 | String se360 = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; 360SE)" ; |
53 | String chrome = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Chrome/9.0.570.0 Safari/534.11" ; |
54 | String safari = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN) AppleWebKit/533.17.8 (KHTML, like Gecko) Version/5.0.1 Safari/533.17.8" ; |
55 | String fireFox= "Mozilla/5.0 (Windows NT 5.2; rv:7.0.1) Gecko/20100101 Firefox/7.0.1" ; |
56 | String opera = "Opera/9.80 (Windows NT 5.2; U; zh-cn) Presto/2.9.168 Version/11.51" ; |
57 | String other = "(Windows NT 5.2; U; zh-cn) Presto/2.9.168 Version/11.51" ; |
58 | BrowseTool b= new BrowseTool(); |
59 | System.out.println(b.checkBrowse(ie9)); |
60 | System.out.println(b.checkBrowse(ie8)); |
61 | System.out.println(b.checkBrowse(ie7)); |
62 | System.out.println(b.checkBrowse(ie6)); |
63 | System.out.println(b.checkBrowse(aoyou)); |
64 | System.out.println(b.checkBrowse(qq)); |
65 | System.out.println(b.checkBrowse(green)); |
66 | System.out.println(b.checkBrowse(se360)); |
67 | System.out.println(b.checkBrowse(chrome)); |
68 | System.out.println(b.checkBrowse(safari)); |
69 | System.out.println(b.checkBrowse(fireFox)); |
70 | System.out.println(b.checkBrowse(opera)); |
71 | System.out.println(b.checkBrowse(other)); |