java验证ip是否合法和获取HttpServletRequest的ip地址,代码如下。
public class IPUtils {
/**
* ip校验
* @param s
* @return Boolean
*/
public static 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 String
*/
public static String getClientAddress(HttpServletRequest request) {
String address = request.getHeader("X-Forwarded-For");
if (address != null &&isIpAddress(address)) {
return address;
}
return request.getRemoteAddr();
}
}