1.校验IP地址
public Boolean isIp(String ip) {
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(ip);
return m.matches();
}
2.获取IP地址
public String getClientAddress(HttpServletRequest request) {
String address = request.getHeader("X-Forwarded-For");
if (address != null && CommonUtils.isIp(address)) {
return address;
}
address=request.getRemoteAddr();
if ("0:0:0:0:0:0:0:1".equals(address)) {
address="localhost";
}
return address;
}
阅读(993) | 评论(0) | 转发(0) |