if (StringUtils.isNotEmpty(address.getIp())) {
if (address.getIp().length()<7||address.getIp().length()>15){
String msg = "请输入合法的IP地址!";
return AjaxResult.error(msg);
}else {
if ( IpAddress.isIP(dkyIpconfSdzzIp.getYwipdz())) {
String dz = address.getIp();
String[] sdzz = dz.split("\\.");
int ip1 = Integer.valueOf(sdzz[0]);
int ip2 = Integer.valueOf(sdzz[1]);
int ip3 = Integer.valueOf(sdzz[2]);
int ip4 = Integer.valueOf(sdzz[3]);
if ((ip1 < 0 || ip1 > 255) || (ip2 < 0 || ip2 > 255) || (ip3 < 0 || ip3 > 255) || (ip4 < 0 || ip4 > 255)) {
String msg = "您输入正常范围内的IP地址!";
return AjaxResult.error(msg);
}
}else {
String msg = "IP地址不正确!";
return AjaxResult.error(msg);
}
}
}
IpAddress类
public class IpAddress {
public static boolean isIP(String addr) {
if (addr==null || addr.length() < 7 || addr.length() > 15 || "".equals(addr)) {
return false;
}
/**
* 判断IP格式和范围
*/
String rexp =
"([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
Pattern pat = Pattern.compile(rexp);
Matcher mat = pat.matcher(addr);
boolean ipAddress = mat.find();
return ipAddress;
}
}