public Map<String,String> getUserAgent(HttpServletRequest req){
Map<String,String> Sys= new HashMap<String, String>();
String ua = req.getHeader("User-Agent").toLowerCase();
String s;
String msieP = "msie ([\\d.]+)";
String firefoxP = "firefox\\/([\\d.]+)";
String chromeP = "chrome\\/([\\d.]+)";
String operaP = "opera.([\\d.]+)/)";
String safariP = "version\\/([\\d.]+).*safari";
Pattern pattern = Pattern.compile(msieP);
Matcher mat = pattern.matcher(ua);
if(mat.find()){
s=mat.group();
Sys.put("type", "ie");
Sys.put("version", s.split(" ")[1]);
return Sys;
}
pattern = Pattern.compile(firefoxP);
mat=pattern.matcher(ua);
if(mat.find()){
s=mat.group();
System.out.println(s);
Sys.put("type", "firefox");
Sys.put("version", s.split("/")[1]);
return Sys;
}
pattern = Pattern.compile(chromeP);
mat=pattern.matcher(ua);
if(mat.find()){
s=mat.group();
Sys.put("type", "chrome");
Sys.put("version", s.split("/")[1]);
return Sys;
}
pattern = Pattern.compile(operaP);
mat=pattern.matcher(ua);
if(mat.find()){
s=mat.group();
Sys.put("type", "opera");
Sys.put("version", s.split("\\.")[1]);
return Sys;
}
pattern = Pattern.compile(safariP);
mat=pattern.matcher(ua);
if(mat.find()){
s=mat.group();
Sys.put("type", "safari");
Sys.put("version", s.split("/")[1].split(".")[0]);
return Sys;
}
return Sys;
}
网上找的一段js获取,原理都是一样,通过浏览器的 User-Agent 获得,可以自由扩展:
/** * 获取浏览器类型及 版本 * @returns 返回对象,两属性,type:表示浏览器类型,version:表示版本 {___anonymous25556_25557} */ function getUserAgent(){ var Sys={}; var ua=navigator.userAgent.toLowerCase(); var s; (s=ua.match(/msie ([\d.]+)/))?(Sys['type']='ie',Sys['version']=s[1]): (s=ua.match(/firefox\/([\d.]+)/))?(Sys['type']='firefox',Sys['version']=s[1]): (s=ua.match(/chrome\/([\d.]+)/))?(Sys['type']='chrome',Sys['version']=s[1]): (s=ua.match(/opera.([\d.]+)/))?(Sys['type']='opera',Sys['version']=s[1]): (s=ua.match(/version\/([\d.]+).*safari/))?(Sys['type']='safari',Sys['version']=s[1]):0; return Sys; }