主要的类
ip类:
IPList中的内容:
package cn.IpUtils;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
* yicha
* Sep 8, 2008
*/
public class IpUtil {
private static final String CONFIG_RESOURCE = "/IPList.txt";
/**
* 从配置文件读取IP区段信息,组成ArrayList返回
*/
public ArrayList getIPSec() {
InputStream ins = IpUtil.class
.getResourceAsStream(CONFIG_RESOURCE);
try {
InputStreamReader fr = new InputStreamReader(ins,"utf-8");
BufferedReader br = new BufferedReader(fr);
String temp = br.readLine();
StringBuffer sb = new StringBuffer();
while (null != temp) {
sb.append(temp);
sb.append("\n");
temp = br.readLine();
}
String[] ips = sb.toString().split("\n");
if (null != sb && ips.length >= 2) {
ArrayList al = new ArrayList();
System.out.println(ips[0]);
for (int i = 0; i < ips.length; i++) {
String[] a = ips[i].split("\t");
IpBean sec = new IpBean();
sec.setFrom(String.valueOf(this.convertIP(a[0])));
sec.setTo(a[1]);
sec.setPosition(a[2]);
sec.setId(i);
al.add(sec);
}
return al;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 将IP转换为数字 位操作
*
* @param ip
* @return
*/
private long convertIP(String ip) {
long result = 0;
String[] temp = ip.split("\\.");
for (int i = 0; i < 4; i++) {
String current = temp[i];
result <<= 8;
result += this.convertStringToInt(current);
}
return result;
}
/**
* 类型转换
*
* @param i
* @return
*/
private int convertStringToInt(String i) {
int result=0;
try{
result=Integer.parseInt(i);
}catch(Exception e){
System.err.println("错误");
}
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
IpUtil iputil=new IpUtil();
//3402407245
// long temp=iputil.convertIP("204.202.157.77");
// System.out.println(temp+"");
IpBean sec ;
ArrayList list=iputil.getIPSec();
for(int i=0;i<80;i++){
sec=(IpBean)list.get(i);
System.out.println(sec.getId()+"\t"+sec.getFrom()+"\t"+sec.getTo()+"\t"+sec.getPosition());
}
}
}
ip类:
package cn.IpUtils;
/**
* yicha
* Sep 8, 2008
*/
public class IpBean {
private String from; // IP段起始
private String to; // IP结束
private String position; // 地理名称
private int id = 0;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
IPList中的内容:
220.194.48.134 220.194.048.134 北京联通
211.137.181.0 211.137.181.000 山东移动
211.137.181.1 211.137.181.001 山东移动
211.137.181.2 211.137.181.002 山东移动
211.137.181.3 211.137.181.003 山东移动
211.137.181.4 211.137.181.004 山东移动
211.137.181.5 211.137.181.005 山东移动