java实现获取各种浏览器版本号及类型
引用
public Map getUserAgent(HttpServletRequest req){
Map Sys= new HashMap();
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;
}
调用
HttpServletRequest request = (HttpServletRequest) Executions
.getCurrent().getNativeRequest();
Map map = getUserAgent(request);
String version = map.get("version");