java 获得客户端真是ip地址
?
/** * ip校验 * @param s * @return 格式是否正确 */ public Boolean isIpAddress(String s){ String regex = "(((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(s); return m.matches(); } /** * 获取客户端ip * @param request * @return ip 地址 */ public String getClientAddress(HttpServletRequest request) { String address = request.getHeader("X-Forwarded-For"); if (address != null && isIpAddress(address)) { return address; } return request.getRemoteAddr(); }