qqwry.bat 下載地址 http://download.csdn.net/detail/takeurhand/8048487
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Name IpAddress
* @Description 根據Ip判斷地理位置
* @Attention NULL
* @Tags
* @Author CaiYj
* <br>2014-10-17
*/
public class IpAddress {
/**
* bat文件地址(這是一個ip庫)
*/
private String dataPath = "src/QQWry.dat";
private RandomAccessFile ipFile = null;
private static IpAddress instance = new IpAddress();
private long ipBegin=0L;
private long ipEnd=0L;
private long ipSum=0L;
private String country="";
private static final int RECORD_LENGTH = 7;
private static final byte AREA_FOLLOWED = 0x01;
private static final byte NO_AREA = 0x02;
private IpAddress() {
try {
ipFile = new RandomAccessFile(new File(dataPath).getAbsolutePath(), "r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(ipFile != null) {
try {
ipBegin = byteArrayToLong(readBytes(0,4));
ipEnd = byteArrayToLong(readBytes(4,4));
if(ipBegin == -1 || ipEnd == -1) {
ipFile.close();
ipFile = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
ipSum = (ipEnd-ipBegin)/RECORD_LENGTH+1;
}
private byte[] readBytes(long offset, int num) {
byte[] ret = new byte[num];
try {
ipFile.seek(offset);
for(int i=0; i != num; i++) {
ret[i] = ipFile.readByte();
}
return ret;
} catch (IOException e) {
e.printStackTrace();
return ret;
}
}
private byte[] readBytes(int num) {
byte[] ret = new byte[num];
try {
for(int i=0; i != num; i++) {
ret[i] = ipFile.readByte();
}
return ret;
} catch (IOException e) {
return ret;
}
}
private long byteArrayToLong(byte[] b) {
long ret = 0;
for(int i=0; i<b.length; i++) {
ret |= ( b[i] << (0x8*i) & (0xFF * (long)(Math.pow(0x100,i))) );
}
return ret;
}
/**
* @Name
* @Description 把ip字符串轉換爲long型
* @Attention NULL
* @ReturnType long NULL
* @Tags @param ip
* @Tags @return
* @Author CaiYj
* <br>2014-10-17
*/
private long StingIpToLong(String ip) {
String[] arr = ip.split("\\.");
return (Long.valueOf(arr[0])*0x1000000 +
Long.valueOf(arr[1])*0x10000 +
Long.valueOf(arr[2])*0x100 +
Long.valueOf(arr[3]));
}
/**
* @Name
* @Description 搜索ip
* @Attention NULL
* @ReturnType long NULL
* @Tags @param ip
* @Tags @return
* @Author CaiYj
* <br>2014-10-17
*/
public long seekIp(String ip) {
long tmp = StingIpToLong(ip);
long i=0;
long j=ipSum;
long m = 0;
long lm=0L;
while(i<j) {
m = (i+j)/2;
lm = m*RECORD_LENGTH + ipBegin;
if( tmp == byteArrayToLong(readBytes(lm, 4))){
return byteArrayToLong(readBytes(3));
}else if(j==(i+1)) {
return byteArrayToLong(readBytes(3));
}else if( tmp > byteArrayToLong(readBytes(lm, 4))){
i = m;
}else/* if( tmp < byteArrayToLong(readBytes(lm, 4)))*/{
j = m;
}
}
return -1L;
}
/**
* @Name
* @Description seekCountryArea di
* @Attention NULL
* @ReturnType String NULL
* @Tags @param offset
* @Tags @return
* @Author CaiYj
* <br>2014-10-17
*/
private String seekCountryArea(long offset) {
try {
ipFile.seek(offset + 4);
byte b = ipFile.readByte();
if(b == AREA_FOLLOWED)
{
long countryOffset = byteArrayToLong(readBytes(3));
ipFile.seek(countryOffset);
b = ipFile.readByte();
if(b == NO_AREA) {
country = readString(byteArrayToLong(readBytes(3)));
ipFile.seek(countryOffset + 4);
} else
country = readString(countryOffset);
//area = readArea(ipFile.getFilePointer());
} else if(b == NO_AREA) {
country = readString(byteArrayToLong(readBytes(3)));
// area = readArea(offset + 8);
} else {
country = readString(ipFile.getFilePointer() - 1);
//area = readArea(ipFile.getFilePointer());
}
return readText(country,"省(.+?)市");//+" "+area;
} catch (IOException e) {
return null;
}
}
/**
* @Name
* @Description readText
* @Attention NULL
* @ReturnType String NULL
* @Tags @param result
* @Tags @param identifier
* @Tags @return
* @Author CaiYj
* <br>2014-10-17
*/
public static String readText(String result, String identifier) {
Pattern shopNumberPattern = Pattern.compile(identifier);
Matcher shopNamMatcher = shopNumberPattern.matcher(result);
if (shopNamMatcher.find())
return shopNamMatcher.group(1);
return "";
}
/**
* @Name
* @Description 從offset偏移處讀取一個以0結束的字符串
* @Attention NULL
* @ReturnType String NULL
* @Tags @param offset
* @Tags @return
* @Author CaiYj
* <br>2014-10-17
*/
private String readString(long offset){
try {
ipFile.seek(offset);
byte[] b = new byte[128];
int i;
for(i=0; (b.length != i) && ((b[i]=ipFile.readByte()) != 0); i++);
String ret = new String(b, 0 , i/*, "GBK"*/);
ret = ret.trim();
return (ret.equals("") ||
ret.indexOf("CZ88.NET") != -1 )?"未知":ret;
} catch (IOException e) {
System.out.println("讀取文件失敗_readString");
}
return "";
}
/**
* @Name IpRecord
* @Description 封裝ip記錄,包括開始ip,結束ip和地址
* @Attention NULL
* @Tags
* @Author CaiYj
* <br>2014-10-17
*/
@SuppressWarnings("unused")
private class IpRecord {
public String beginIp;
public String endIp;
public String address;
public IpRecord() {
beginIp = endIp = address = "";
}
public String toString() {
return beginIp + " - " + endIp + " " + address;
}
}
/**
* @Name
* @Description Singleton
* @Attention NULL
* @ReturnType IpAddress NULL
* @Tags @return
* @Author CaiYj
* <br>2014-10-17
*/
public static IpAddress getInstance() {
return instance;
}
/**
* @Name
* @Description NULL
* @Attention NULL
* @ReturnType String NULL
* @Tags @param ip
* @Tags @return
* @Author CaiYj
* <br>2014-10-17
*/
public String IpStringToAddress(String ip) {
long ipOffset = seekIp(ip);
String ret = seekCountryArea(ipOffset);
return ret;
}
/**
* @Name
* @Description NULL
* @Attention NULL
* @ReturnType long ipSum
* @Tags @return
* @Author CaiYj
* <br>2014-10-17
*/
public long getIpSum() {
return ipSum;
}
public static void main(String[] args) throws UnknownHostException {
IpAddress ipAddr = IpAddress.getInstance();
// /**
// * ip總數 long
// */
// long l = ipAddr.getIpSum();
// System.out.println(l);
String str = ipAddr.IpStringToAddress("255.255.255.0");
System.out.println("255.255.255.0 "+str);
str = ipAddr.IpStringToAddress("113.235.124.128");
System.out.println("113.235.124.128 "+str);
str = ipAddr.IpStringToAddress("192.168.0.174");
System.out.println("192.168.0.174"+str);
InetAddress inet = InetAddress.getLocalHost();
System.out.println("本地ip" + inet.getHostAddress());
}
}