/** * 计算传入的IP地址的数字IP*/ public static long getIpNum(String ip) { long ipNum = 0; if (StringUtils.isNotBlank(ip) && isIP(ip)) { String[] spstr_IP = ip.split("\."); ipNum = Long.parseLong(spstr_IP[0]) * 256 * 256 * 256 + Long.parseLong(spstr_IP[1]) * 256 * 256 + Long.parseLong(spstr_IP[2]) * 256 + Long.parseLong(spstr_IP[3]); } return ipNum; } /** * 判断是否是合法的IP地址*/ public static boolean isIP(String ip){ Pattern ipPattern=Pattern.compile("([1-9]|[1-9]\d|1\d{2}|2[0-1]\d|22[0-3])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}"); Matcher matcher=ipPattern.matcher(ip); return matcher.matches(); }