62,567
社区成员




public class Test {
public static void main(String []args){
String ipRule = "192.168.*.*";
String ipReg = ipRule.replace(".", "\\.").replace("*", "(([0-9])|([1-9][0-9])|([1-2][0-5][0-5]))");//把IP规则替换为正则
String []testIps = new String[]{"192.168.1.555","192.168.256.1","192.168.253.10"};
for(String tempIp:testIps){
if(tempIp.matches(ipReg)){
System.out.println("合法IP: "+tempIp);
}
}
}
}
package com.saturday.string;
public class IPMatcher {
public static void main(String[] args){
String allowIp="192.168.245.*;192.168.125.*;192.168.251.254;192.168.*.*;192.*.*.*;";
String ip="192.168.251.111";
System.out.println(isIPAllow(allowIp,ip));
}
public static boolean isIPAllow(
String allowIp,
String ip){
String
s1=ip,
s2=ip.replaceAll("\\.\\d+$", ".*"),
s3=ip.replaceAll("\\.\\d+\\.\\d+$", ".*.*"),
s4="*.*.*.*";
if(allowIp.indexOf(s1)>-1) return true;
if(allowIp.indexOf(s2)>-1) return true;
if(allowIp.indexOf(s3)>-1) return true;
if(allowIp.indexOf(s4)>-1) return true;
return false;
}
}
//将127.0.0.1形式的IP地址转换成十进制整数,这里没有进行任何错误处理
public static long ipToLong(String strIp) ...{
long[] ip = new long[4];
//先找到IP地址字符串中.的位置
int position1 = strIp.indexOf(".");
int position2 = strIp.indexOf(".", position1 + 1);
int position3 = strIp.indexOf(".", position2 + 1);
//将每个.之间的字符串转换成整型
ip[0] = Long.parseLong(strIp.substring(0, position1));
ip[1] = Long.parseLong(strIp.substring(position1+1, position2));
ip[2] = Long.parseLong(strIp.substring(position2+1, position3));
ip[3] = Long.parseLong(strIp.substring(position3+1));
return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
}
1. ID NUMBER(9) PK
2. START_IP VARCHAR2(15)
3. END_IP VARCHAR2(15)
4. START_IP_VALUE NUMBER(10) 根据 START_IP 转成的数值
5. END_IP_VALUE NUMBER(10) 根据 END_IP 转成的数值
ID START_IP END_IP START_IP_VALUE END_IP_VALUE
------------------------------------------------------------------
1 192.168.245.0 192.168.245.255 3232298240 3232298495
2 192.168.125.0 192.168.125.255 3232267520 3232267775
3 192.168.251.254 192.168.251.254 3232300030 3232300030
4 192.168.0.0 192.168.255.255 3232235520 3232301055
5 192.0.0.0 192.255.255.255 3221225472 3238002687
public class IpTest {
public static void main(String[] args) {
String ips = "192.*.*.*;192.168.125.*;192.168.251.254;192.168.245.*;192.168.*.*";
IpContent content = new IpContent(ips);
System.out.println(content.contains("193.173.0.1"));
}
}
import java.util.Arrays;
import java.util.regex.Pattern;
public class IpContent {
private final static String DEFAULT_SEPARATOR = ";";
private IpScope[] ipScopes;
public IpContent(String ips) {
this( ips, DEFAULT_SEPARATOR);
}
public IpContent(String ips, String separator) {
this( ips.split(Pattern.quote(separator)) );
}
public IpContent(String[] ipsArray) {
init(ipsArray);
}
public boolean contains(String ip) {
if(ipScopes == null || ipScopes.length == 0) {
return false;
}
long ipValue = IpUtil.ip2Number(ip);
for(int i = 0; i < ipScopes.length; i++) {
if(ipScopes[i].contains(ipValue)) {
return true;
}
}
return false;
}
private void init(String[] ipsArray) {
if(ipsArray == null || ipsArray.length == 0) {
return;
}
ipScopes = new IpScope[ipsArray.length];
for(int i = 0; i < ipsArray.length; i++) {
ipScopes[i] = new IpScope(ipsArray[i]);
}
Arrays.sort(ipScopes);
}
public String toString() {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < ipScopes.length; i++) {
sb.append(ipScopes[i]).append("\n");
}
return sb.toString();
}
}
public class IpScope implements Comparable<IpScope> {
public final static String FUZZY_PLACEHOLDER = "*";
public final static String IP_MIN_VALUE = "0";
public final static String IP_MAX_VALUE = "255";
private String ip;
private long startIpValue;
private long endIpValue;
IpScope(String ip) {
this.ip = ip;
processIpValue();
}
public String getIp() {
return ip;
}
public String getStartIp() {
return IpUtil.number2Ip(startIpValue);
}
public String getEndIp() {
return IpUtil.number2Ip(endIpValue);
}
public long getStartIpValue() {
return startIpValue;
}
public long getEndIpValue() {
return endIpValue;
}
public boolean contains(long ipValue) {
return (ipValue >= startIpValue) && (ipValue <= endIpValue);
}
private long diff() {
return endIpValue - startIpValue;
}
private void processIpValue() {
String[] ips = ip.split("\\.");
startIpValue = toNumber(ips.clone(), IP_MIN_VALUE);
endIpValue = toNumber(ips.clone(), IP_MAX_VALUE);
}
private long toNumber(String[] ips, String defaultValue) {
for(int i = 0; i < ips.length; i++) {
ips[i] = ips[i].trim();
if(FUZZY_PLACEHOLDER.equals(ips[i])) {
ips[i] = defaultValue;
}
}
return IpUtil.ip2Number(ips);
}
public int compareTo(IpScope o) {
long diff = this.diff() - o.diff();
if(diff > Integer.MAX_VALUE) {
return Integer.MIN_VALUE;
}
return -(int)diff;
}
public String toString() {
return "IP pattern: " + ip + ", start: " + getStartIp() + ", end: " + getEndIp();
}
}
public class IpUtil {
/**
* 将 IP 字符串转为 long 数据
* @param ip
* @return
* @author frankiegao123
* 2010-2-26 下午03:47:14
*/
public static long ip2Number(String ip) {
String[] s = ip.split("\\.");
return ip2Number(s);
}
public static long ip2Number(String[] ips) {
long ipn = 0L;
for (int i = 0; i < ips.length; i++) {
ipn = (ipn << 8) | Long.parseLong(ips[i]);
}
return ipn;
}
/**
* 以字符串形式表示的 IP 地址
* @param number
* @return
* @author frankiegao123
* 2010-2-26 下午03:47:35
*/
public static String number2Ip(long number) {
char[] chs = new char[15];
int offset = 0;
for (int i = 1; i <= 4; i++) {
if(i > 1) {
chs[offset++] = '.';
}
int shift = (4 - i) * 8;
int n = (int) ((number & (0xff << shift)) >>> shift);
offset = putChar(chs, offset, n);
}
return new String(chs, 0, offset);
}
private static int putChar(char[] chs, int offset, int number) {
int len = length(number);
int t = len;
while(t > 0) {
chs[--t + offset] = (char)('0' + number % 10);
number /= 10;
}
return offset + len;
}
private static int length(int number) {
int len = 1;
while(number > 9) {
number /= 10;
len++;
}
return len;
}
}