标签:
获取客户端真实IP
package org.j4.utils; import java.util.regex.Matcher; public class WebUtils { /** * 取得客户端真实ip */ public static String getIpAddr() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder .getRequestAttributes()).getRequest(); String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } private final static String IE10 = "MSIE 10.0"; private final static String IE9 = "MSIE 9.0"; private final static String IE8 = "MSIE 8.0"; private final static String IE7 = "MSIE 7.0"; private final static String IE6 = "MSIE 6.0"; private final static String MAXTHON = "Maxthon"; private final static String QQ = "QQBrowser"; private final static String GREEN = "GreenBrowser"; private final static String SE360 = "360SE"; private final static String FIREFOX = "Firefox"; private final static String OPERA = "Opera"; private final static String CHROME = "Chrome"; private final static String SAFARI = "Safari"; private final static String OTHER = "其它"; public static String getBrowse() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder .getRequestAttributes()).getRequest(); String userAgent = request.getHeader("USER-AGENT"); if (regex(OPERA, userAgent)) return OPERA; if (regex(CHROME, userAgent)) return CHROME; if (regex(FIREFOX, userAgent)) return FIREFOX; if (regex(SAFARI, userAgent)) return SAFARI; if (regex(SE360, userAgent)) return SE360; if (regex(GREEN, userAgent)) return GREEN; if (regex(QQ, userAgent)) return QQ; if (regex(MAXTHON, userAgent)) return MAXTHON; if (regex(IE10, userAgent)) return IE10; if (regex(IE9, userAgent)) return IE9; if (regex(IE8, userAgent)) return IE8; if (regex(IE7, userAgent)) return IE7; if (regex(IE6, userAgent)) return IE6; return OTHER; } public static boolean regex(String regex, String str) { Pattern p = Pattern.compile(regex, Pattern.MULTILINE); Matcher m = p.matcher(str); return m.find(); } }
标签:
原文地址:http://my.oschina.net/liting/blog/401839