做了一段时间的数据统计功能,无论是apache,还是tomcat,在记录日志的过程中都有IP被记录,目前大多数软件的后台都要求统计一下,数据的来源是从哪些城市过来的,所以要用到IP库。以前一直以为这个东西一定是购买的,后来在网上终于找到了免费的,还是还位仁兄特意写了java的代码用来参考!在些表示感谢!
转载地址:
http://www.blogjava.net/alvin/archive/2007/08/23/141139.html
import java.io.*;
public class QQWry {
private String DbPath = "D:\\Downloads\\IPLook\\QQWry.Dat" + ""; // 纯真IP数据库地址
private String Country, LocalStr;
private long IPN;
private int RecordCount, CountryFlag;
private long RangE, RangB, OffSet, StartIP, EndIP, FirstStartIP,
LastStartIP, EndIPOff;
private RandomAccessFile fis;
private byte[] buff;
private long B2L(byte[] b) {
long ret = 0;
for (int i = 0; i < b.length; i++) {
long t = 1L;
for (int j = 0; j < i; j++)
t = t * 256L;
ret += ((b[i] < 0) ? 256 + b[i] : b[i]) * t;
}
return ret;
}
private long ipToInt(String ip) {
String[] arr = ip.split("\\.");
long ret = 0;
for (int i = 0; i < arr.length; i++) {
long l = 1;
for (int j = 0; j < i; j++)
l *= 256;
try {
ret += Long.parseLong(arr[arr.length - i - 1]) * l;
} catch (Exception e) {
ret += 0;
}
}
return ret;
}
public void seek(String ip) throws Exception {
this.IPN = ipToInt(ip);
fis = new RandomAccessFile(this.DbPath, "r");
buff = new byte[4];
fis.seek(0);
fis.read(buff);
FirstStartIP = this.B2L(buff);
fis.read(buff);
LastStartIP = this.B2L(buff);
RecordCount = (int) ((LastStartIP - FirstStartIP) / 7);
if (RecordCount <= 1) {
LocalStr = Country = "未知";
throw new Exception();
}
RangB = 0;
RangE = RecordCount;
long RecNo;
do {
RecNo = (RangB + RangE) / 2;
getStartIP(RecNo);
if (IPN == StartIP) {
RangB = RecNo;
break;
}
if (IPN > StartIP)
RangB = RecNo;
else
RangE = RecNo;
} while (RangB < RangE - 1);
getStartIP(RangB);
getEndIP();
getCountry(IPN);
fis.close();
}
private String getFlagStr(long OffSet) throws IOException {
int flag = 0;
do {
fis.seek(OffSet);
buff = new byte[1];
fis.read(buff);
flag = (buff[0] < 0) ? 256 + buff[0] : buff[0];
if (flag == 1 || flag == 2) {
buff = new byte[3];
fis.read(buff);
if (flag == 2) {
CountryFlag = 2;
EndIPOff = OffSet - 4;
}
OffSet = this.B2L(buff);
} else
break;
} while (true);
if (OffSet < 12) {
return "";
} else {
fis.seek(OffSet);
return getStr();
}
}
private String getStr() throws IOException {
long l = fis.length();
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
byte c = fis.readByte();
do {
byteout.write(c);
c = fis.readByte();
} while (c != 0 && fis.getFilePointer() < l);
return byteout.toString();
}
private void getCountry(long ip) throws IOException {
if (CountryFlag == 1 || CountryFlag == 2) {
Country = getFlagStr(EndIPOff + 4);
if (CountryFlag == 1) {
LocalStr = getFlagStr(fis.getFilePointer());
if (IPN >= ipToInt("255.255.255.0")
&& IPN <= ipToInt("255.255.255.255")) {
LocalStr = getFlagStr(EndIPOff + 21);
Country = getFlagStr(EndIPOff + 12);
}
} else {
LocalStr = getFlagStr(EndIPOff +
;
}
} else {
Country = getFlagStr(EndIPOff + 4);
LocalStr = getFlagStr(fis.getFilePointer());
}
}
private long getEndIP() throws IOException {
fis.seek(EndIPOff);
buff = new byte[4];
fis.read(buff);
EndIP = this.B2L(buff);
buff = new byte[1];
fis.read(buff);
CountryFlag = (buff[0] < 0) ? 256 + buff[0] : buff[0];
return EndIP;
}
private long getStartIP(long RecNo) throws IOException {
OffSet = FirstStartIP + RecNo * 7;
fis.seek(OffSet);
buff = new byte[4];
fis.read(buff);
StartIP = this.B2L(buff);
buff = new byte[3];
fis.read(buff);
EndIPOff = this.B2L(buff);
return StartIP;
}
public String getLocal() {
return this.LocalStr;
}
public String getCountry() {
return this.Country;
}
public void setPath(String path) {
this.DbPath = path;
}
public static void main(String[] args) throws Exception {
QQWry w = new QQWry();
// w.setPath(new File("QQWry2.Dat").getAbsolutePath());
w.seek("219.232.233.103");
System.out.println(w.getCountry() + " " + w.getLocal());
}
}
分享到:
- 2008-04-02 15:52
- 浏览 1496
- 评论(0)
- 论坛回复 / 浏览 (0 / 4197)
- 查看更多
相关推荐
软件:中国知音IP库查询 软件说明:类似:纯真IP库查询工具,但是我加了IP段搜索出来自动处理功能,导出保存功能。我觉得会方便大家使用。 By:中国知音 http://www.bycnboy.com
根据相应IP地址,能查询到对应的IP所在地,里面有相应的IP库调用API,大家可以下载,类注释写的很详细...
VB版的纯真IP库查询小程序,需要自己手动更新纯真IP库
运用Python脚本对纯真IP库进行 IP查询,导出文本
IP库名称为:qqwry.dat 升级IP库:update_ip.exe 让update_ip.exe和qqwry.dat同一目录下 update_ip.exe用于升级QQip库的,每隔五天可以升级一次 先在本机电脑升级后,再把升级完的IP库上传到你的...
查询手机号码归属地和ip归属地 还不错 能用了。。。。。。。。。
抓到鸡的多少和IP活不活跃是有密切联系的! 自己去感觉!本资源仅供参考学习!
这是一款ip整理工具 配合纯真使用的
PHP利用纯真IP数据库在本地实现IP地址信息查询,返回结果无乱码,代码稳定可靠,2020.08.28日出炉的代码
C# IP库地址查询 C# IP库地址查询
用于查询纯真IP数据库(http://www.cz88.net) 的程序。从LumaQQ的Java程序段翻译过来。现在从地址反查到IP的速度还比较慢,又进一步优化的必要。原程序内含原Java代码供参考。用的是VS2005。VS2003无法打开项目,可以...
活跃段纯真IP查找库 请自行杀毒!!!
全球ip信息免费查询的工具类及相关的ip库文件,已经配有相关的测试类使用方法,欢迎下载
使用纯真IP库的bat文件实现IP查询,具体情况自己看吧~
全球IP地址,具有IP地址查询功能和搜索功能,可以参考,
IP数据库 手机归属地查询软件完整源代码 C# v1.0
纯真IP数据库纯真IP数据库 查询IP纯真IP数据库 查询IP
包含44万余条IP段信息 比之前csdn上的IP库更全 部分IP能精确到区县 并包含国外IP 将真实ip转换为数字ip: 运算将真实IP转换为数字 比如北京的IP段是 : 1 1 0 0 1 1 0 255 Startip: 1 256 256 256+1 256 256+0 256...
ASP.NET的IP地址查询程序,附有最新的纯真IP地址库,日期是200年11月5日,导出有数据库自动升级程序,可以适时自动更新IP库。
可根据IP地址查询国家,地区,地址