资源简介
IpRangeUtil;java 校验Ip段(子网掩码);
支持 iptables.patterns=10.0.0.0/8,192.168.0.0/16,172.16.0.0/12,202.195.129.240/28,202.195.128.0/20
代码片段和文件信息
package services.announcement.util;
import com.wisorg.scc.core.bean.impl.FastjsonConverter;
import com.wisorg.scc.core.util.StringUtils;
import java.util.regex.Pattern;
/**
* Created by stzhang on 2015/10/13.
*/
public class IpRangeUtil {
private static final String IPV4_REGEX = “((\\d|[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})“;
public static boolean checkIpField(String ip1 String ip2Mask){
int idex = ip2Mask.indexOf(“/“);
if(idex == -1) return false;
String maskStr = ip2Mask.substring(idex + 1);
if(StringUtils.isEmpty(maskStr)) return false;
int mask = Integer.parseInt(maskStr);
String ip2 = ip2Mask.substring(0 idex);
return checkSameSegment(ip1 ip2 getMaskInt(mask));
}
private static boolean checkSameSegment(String ip1 String ip2 int mask) {
// 判断IPV4是否合法
if (!ipV4Validate(ip1)) {
return false;
}
if (!ipV4Validate(ip2)) {
return false;
}
int ipValue1 = getIpV4Value(ip1);
int ipValue2 = getIpV4Value(ip2);
return (mask & ipValue1) == (mask & ipValue2);
}
private static int getMaskInt(int maskLength) {
int subLengt
评论
共有 条评论