`
- 浏览:
244730 次
- 性别:
- 来自:
杭州
-
java 读取纯真IP数据库QQwry.dat的源代码(转)
-
java读取纯真IP数据库QQwry.dat的源代码,要运行此程序必须到网上下载QQwry.da,由于太大,我这里就不提供了。
-
一、IPEntry.java
-
-
-
-
-
-
-
public
class
IPEntry {
-
public
String beginIp;
-
public
String endIp;
-
public
String country;
-
public
String area;
-
-
-
-
-
-
-
public
IPEntry() {
-
beginIp = endIp = country = area = ""
;
-
}
-
-
public
String toString(){
-
retur this
.area+
" "
+
this
.country+
"IP范围:"
+
this
.beginIp+
"-"
+
this
.endIp;
-
}
-
}
-
-
二、Utils.java
-
-
-
-
-
-
import
java.io.UnsupportedEncodingException;
-
-
-
-
public
class
Utils {
-
-
-
-
-
-
public
static
byte
[] getIpByteArrayFromString(String ip) {
-
byte
[] ret =
new
byte
[
4
];
-
java.util.StringTokenizer st = new
java.util.StringTokenizer(ip,
"."
);
-
try
{
-
ret[0
] = (
byte
)(Integer.parseInt(st.nextToken()) &
0xFF
);
-
ret[1
] = (
byte
)(Integer.parseInt(st.nextToken()) &
0xFF
);
-
ret[2
] = (
byte
)(Integer.parseInt(st.nextToken()) &
0xFF
);
-
ret[3
] = (
byte
)(Integer.parseInt(st.nextToken()) &
0xFF
);
-
} catch
(Exception e) {
-
System.out.println(e.getMessage());
-
}
-
return
ret;
-
}
-
-
public
static
void
main(String args[]){
-
byte
[] a=getIpByteArrayFromString(args[
0
]);
-
for
(
int
i=
0
;i< a.length;i++)
-
System.out.println(a[i]);
-
System.out.println(getIpStringFromBytes(a));
-
}
-
-
-
-
-
-
-
-
public
static
String getString(String s, String srcEncoding, String destEncoding) {
-
try
{
-
return
new
String(s.getBytes(srcEncoding), destEncoding);
-
} catch
(UnsupportedEncodingException e) {
-
return
s;
-
}
-
}
-
-
-
-
-
-
-
-
public
static
String getString(
byte
[] b, String encoding) {
-
try
{
-
return
new
String(b, encoding);
-
} catch
(UnsupportedEncodingException e) {
-
return
new
String(b);
-
}
-
}
-
-
-
-
-
-
-
-
-
-
public
static
String getString(
byte
[] b,
int
offset,
int
len, String encoding) {
-
try
{
-
return
new
String(b, offset, len, encoding);
-
} catch
(UnsupportedEncodingException e) {
-
return
new
String(b, offset, len);
-
}
-
}
-
-
-
-
-
-
public
static
String getIpStringFromBytes(
byte
[] ip) {
-
StringBuffer sb = new
StringBuffer();
-
sb.append(ip[0
] &
0xFF
);
-
sb.append('.'
);
-
sb.append(ip[1
] &
0xFF
);
-
sb.append('.'
);
-
sb.append(ip[2
] &
0xFF
);
-
sb.append('.'
);
-
sb.append(ip[3
] &
0xFF
);
-
return
sb.toString();
-
}
-
}
-
-
三、IPSeeker.java
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
import
java.io.FileNotFoundException;
-
import
java.io.IOException;
-
import
java.io.RandomAccessFile;
-
import
java.nio.ByteOrder;
-
import
java.nio.MappedByteBuffer;
-
import
java.nio.channels.FileChannel;
-
import
java.util.ArrayList;
-
import
java.util.Hashtable;
-
import
java.util.List;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public
class
IPSeeker {
-
-
-
-
-
-
-
-
-
-
-
private
class
IPLocation {
-
public
String country;
-
public
String area;
-
public
IPLocation() {
-
country = area = ""
;
-
}
-
public
IPLocation getCopy() {
-
IPLocation ret = new
IPLocation();
-
ret.country = country;
-
ret.area = area;
-
return
ret;
-
}
-
}
-
private
static
final
String IP_FILE = IPSeeker.
class
.getResource(
"/QQWry.dat"
).toString().substring(
5
);
-
-
private
static
final
int
IP_RECORD_LENGTH =
7
;
-
private
static
final
byte
AREA_FOLLOWED =
0x01
;
-
private
static
final
byte
NO_AREA =
0x2
;
-
-
private
Hashtable ipCache;
-
-
private
RandomAccessFile ipFile;
-
-
private
MappedByteBuffer mbb;
-
-
private
static
IPSeeker instance =
new
IPSeeker();
-
-
private
long
ipBegin, ipEnd;
-
-
private
IPLocation loc;
-
private
byte
[] buf;
-
private
byte
[] b4;
-
private
byte
[] b3;
-
-
-
-
private
IPSeeker() {
-
ipCache = new
Hashtable();
-
loc = new
IPLocation();
-
buf = new
byte
[
100
];
-
b4 = new
byte
[
4
];
-
b3 = new
byte
[
3
];
-
try
{
-
ipFile = new
RandomAccessFile(IP_FILE,
"r"
);
-
} catch
(FileNotFoundException e) {
-
System.out.println(IPSeeker.class
.getResource(
"/QQWry.dat"
).toString());
-
System.out.println(IP_FILE);
-
System.out.println("IP地址信息文件没有找到,IP显示功能将无法使用"
);
-
ipFile = null
;
-
}
-
-
if
(ipFile !=
null
) {
-
try
{
-
ipBegin = readLong4(0
);
-
ipEnd = readLong4(4
);
-
if
(ipBegin == -
1
|| ipEnd == -
1
) {
-
ipFile.close();
-
ipFile = null
;
-
}
-
} catch
(IOException e) {
-
System.out.println("IP地址信息文件格式有错误,IP显示功能将无法使用"
);
-
ipFile = null
;
-
}
-
}
-
}
-
-
-
-
public
static
IPSeeker getInstance() {
-
return
instance;
-
}
-
-
-
-
-
-
public
List getIPEntriesDebug(String s) {
-
List ret = new
ArrayList();
-
long
endOffset = ipEnd +
4
;
-
for
(
long
offset = ipBegin +
4
; offset <= endOffset; offset += IP_RECORD_LENGTH) {
-
-
long
temp = readLong3(offset);
-
-
if
(temp != -
1
) {
-
IPLocation loc = getIPLocation(temp);
-
-
if
(loc.country.indexOf(s) != -
1
|| loc.area.indexOf(s) != -
1
) {
-
IPEntry entry = new
IPEntry();
-
entry.country = loc.country;
-
entry.area = loc.area;
-
-
readIP(offset - 4
, b4);
-
entry.beginIp = Utils.getIpStringFromBytes(b4);
-
-
readIP(temp, b4);
-
entry.endIp = Utils.getIpStringFromBytes(b4);
-
-
ret.add(entry);
-
}
-
}
-
}
-
return
ret;
-
}
-
-
-
-
-
-
public
List getIPEntries(String s) {
-
List ret = new
ArrayList();
-
try
{
-
-
if
(mbb ==
null
) {
-
FileChannel fc = ipFile.getChannel();
-
mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0
, ipFile.length());
-
mbb.order(ByteOrder.LITTLE_ENDIAN);
-
}
-
int
endOffset = (
int
)ipEnd;
-
for
(
int
offset = (
int
)ipBegin +
4
; offset <= endOffset; offset += IP_RECORD_LENGTH) {
-
int
temp = readInt3(offset);
-
if
(temp != -
1
) {
-
IPLocation loc = getIPLocation(temp);
-
-
if
(loc.country.indexOf(s) != -
1
|| loc.area.indexOf(s) != -
1
) {
-
IPEntry entry = new
IPEntry();
-
entry.country = loc.country;
-
entry.area = loc.area;
-
-
readIP(offset - 4
, b4);
-
entry.beginIp = Utils.getIpStringFromBytes(b4);
-
-
readIP(temp, b4);
-
entry.endIp = Utils.getIpStringFromBytes(b4);
-
-
ret.add(entry);
-
}
-
}
-
}
-
} catch
(IOException e) {
-
System.out.println(e.getMessage());
-
}
-
return
ret;
-
}
-
-
-
-
-
-
private
int
readInt3(
int
offset) {
-
mbb.position(offset);
-
return
mbb.getInt() &
0x00FFFFFF
;
-
}
-
-
-
-
-
private
int
readInt3() {
-
return
mbb.getInt() &
0x00FFFFFF
;
-
}
-
-
-
-
-
-
public
String getCountry(
byte
[] ip) {
-
-
if
(ipFile ==
null
)
return
"错误的IP数据库文件"
;
-
-
String ipStr = Utils.getIpStringFromBytes(ip);
-
-
if
(ipCache.containsKey(ipStr)) {
-
IPLocation loc = (IPLocation)ipCache.get(ipStr);
-
return
loc.country;
-
} else
{
-
IPLocation loc = getIPLocation(ip);
-
ipCache.put(ipStr, loc.getCopy());
-
return
loc.country;
-
}
-
}
-
-
-
-
-
-
public
String getCountry(String ip) {
-
return
getCountry(Utils.getIpByteArrayFromString(ip));
-
}
-
-
-
-
-
-
public
String getArea(
byte
[] ip) {
-
-
if
(ipFile ==
null
)
return
"错误的IP数据库文件"
;
-
-
String ipStr = Utils.getIpStringFromBytes(ip);
-
-
if
(ipCache.containsKey(ipStr)) {
-
IPLocation loc = (IPLocation)ipCache.get(ipStr);
-
return
loc.area;
-
} else
{
-
IPLocation loc = getIPLocation(ip);
-
ipCache.put(ipStr, loc.getCopy());
-
return
loc.area;
-
}
-
}
-
-
-
-
-
-
public
String getArea(String ip) {
-
return
getArea(Utils.getIpByteArrayFromString(ip));
-
}
-
-
-
-
-
-
private
IPLocation getIPLocation(
byte
[] ip) {
-
IPLocation info = null
;
-
long
offset = locateIP(ip);
-
if
(offset != -
1
)
-
info = getIPLocation(offset);
-
if
(info ==
null
) {
-
info = new
IPLocation();
-
info.country = "未知国家"
;
-
info.area = "未知地区"
;
-
}
-
return
info;
-
}
-
-
-
-
-
-
-
private
long
readLong4(
long
offset) {
-
long
ret =
0
;
-
try
{
-
ipFile.seek(offset);
-
ret |= (ipFile.readByte() & 0xFF
);
-
ret |= ((ipFile.readByte() << 8
) &
0xFF00
);
-
ret |= ((ipFile.readByte() << 16
) &
0xFF0000
);
-
ret |= ((ipFile.readByte() << 24
) &
0xFF000000
);
-
return
ret;
-
} catch
(IOException e) {
-
return
-
1
;
-
}
-
}
-
-
-
-
-
-
-
private
long
readLong3(
long
offset) {
-
long
ret =
0
;
-
try
{
-
ipFile.seek(offset);
-
ipFile.readFully(b3);
-
ret |= (b3[0
] &
0xFF
);
-
ret |= ((b3[1
] <<
8
) &
0xFF00
);
-
ret |= ((b3[2
] <<
16
) &
0xFF0000
);
-
return
ret;
-
} catch
(IOException e) {
-
return
-
1
;
-
}
-
}
-
-
-
-
-
private
long
readLong3() {
-
long
ret =
0
;
-
try
{
-
ipFile.readFully(b3);
-
ret |= (b3[0
] &
0xFF
);
-
ret |= ((b3[1
] <<
8
) &
0xFF00
);
-
ret |= ((b3[2
] <<
16
) &
0xFF0000
);
-
return
ret;
-
} catch
(IOException e) {
-
return
-
1
;
-
}
-
}
-
-
-
-
-
-
-
private
void
readIP(
long
offset,
byte
[] ip) {
-
try
{
-
ipFile.seek(offset);
-
ipFile.readFully(ip);
-
byte
temp = ip[
0
];
-
ip[0
] = ip[
3
];
-
ip[3
] = temp;
-
temp = ip[1
];
-
ip[1
] = ip[
2
];
-
ip[2
] = temp;
-
} catch
(IOException e) {
-
System.out.println(e.getMessage());
-
}
-
}
-
-
-
-
-
-
-
private
void
readIP(
int
offset,
byte
[] ip) {
-
mbb.position(offset);
-
mbb.get(ip);
-
byte
temp = ip[
0
];
-
ip[0
] = ip[
3
];
-
ip[3
] = temp;
-
temp = ip[1
];
-
ip[1
] = ip[
2
];
-
ip[2
] = temp;
-
}
-
-
-
-
-
-
-
private
int
compareIP(
byte
[] ip,
byte
[] beginIp) {
-
for
(
int
i =
0
; i <
4
; i++) {
-
int
r = compareByte(ip[i], beginIp[i]);
-
if
(r !=
0
)
-
return
r;
-
}
-
return
0
;
-
}
-
-
-
-
-
-
-
private
int
compareByte(
byte
b1,
byte
b2) {
-
if
((b1 &
0xFF
) > (b2 &
0xFF
))
-
return
1
;
-
else
if
((b1 ^ b2) ==
0
)
-
return
0
;
-
else
-
return
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
最新纯真ip数据库qqwry.dat
NULL 博文链接:https://songjianyong.iteye.com/blog/1500127
纯真IP数据库QQwry.dat可供网站、软件等相关程序语言调用读取指定IP的城市地区信息
记录总数:529072条 更新日期:2021年04月21日 数据库版本:纯真
纯真IP数据库 QQwry.Dat 090630版
qqwry.dat 纯真ip数据库 附在线升级工具 下载下来直接安装即可,ip数据库(qqwry.dat)在安装所在目录下
PHP 根据IP地址获取所在城市代码,通过调用纯真ip库qqwry.dat,判断所在城市。
纯真IP数据库qqwry.dat20111130版
纯真数据库 qqwry.dat 200809
最新纯真IP数据库 ,最后更新时间:2009-09-05
包含一个QQWry.Dat,ShowIP.exe QQWry.Dat主要包含IP数据库 ShowIP.exe可以在线升级dat数据库,在可以查看IP地区
QQ 纯真IP数据库.QQ 纯真IP数据库qqwry.rar
qqwry.dat, 纯真IP地址数据库镜像,mirror of qqwry.dat.zip
http://blog.csdn.net/thewindkee/article/details/70171711
记录总数:527271条 更新日期:2019年5月30日 数据库版本:纯真 gitee:https://gitee.com/aizhineng/Pure-IP-database/tree/master/2019/05/30
2021年最新纯真IP地址数据库qqwry.dat
纯真IP库(qqwry.dat) java解析纯真IP数据库……
给定IP地址,在qqwry.DAT中查询对应的IP地址信息。 VS2010+WIN7