/*** 从纯真IP地址库查询ip归属*/

public classIPSeeker {/*** * 用来封装ip相关信息,目前只有两个字段,ip所在的国家和地区*/

private classIPLocation {publicString country;publicString area;publicIPLocation() {

country= area = "";

}publicIPLocation getCopy() {

IPLocation ret= newIPLocation();

ret.country=country;

ret.area=area;returnret;

}

}//一些固定常量,比如记录长度等等

private static final int IP_RECORD_LENGTH = 7;private static final byte AREA_FOLLOWED = 0x01;private static final byte NO_AREA = 0x2;//用来做为cache,查询一个ip时首先查看cache,以减少不必要的重复查找

privateHashtable ipCache;//随机文件访问类

privateRandomAccessFile ipFile;//内存映射文件

privateMappedByteBuffer mbb;//单一模式实例

private static IPSeeker instance = null;//起始地区的开始和结束的绝对偏移

private longipBegin, ipEnd;//为提高效率而采用的临时变量

privateIPLocation loc;private byte[] buf;private byte[] b4;private byte[] b3;/*** 私有构造函数*/

privateIPSeeker(String ip_file) {//System.out.println("初始化开始了");

ipCache = newHashtable();

loc= newIPLocation();

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(ip_file).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;

}

}

}/***@paramipFile 纯真数据库.dat的IP文件路径;默认在conf/QQWry.dat

*@return单一实例*/

public staticIPSeeker getInstance(String ipFile) {

instance= instance==null ?newIPSeeker(ipFile):instance;returninstance;

}/*** 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录

*

*@params

* 地点子串

*@return包含IPEntry类型的List*/

publicList getIPEntriesDebug(String s) {

List ret= newArrayList();long endOffset = ipEnd + 4;for (long offset = ipBegin + 4; offset <= endOffset; offset +=IP_RECORD_LENGTH) {//读取结束IP偏移

long temp =readLong3(offset);//如果temp不等于-1,读取IP的地点信息

if (temp != -1) {

IPLocation loc=getIPLocation(temp);//判断是否这个地点里面包含了s子串,如果包含了,添加这个记录到List中,如果没有,继续

if (loc.country.indexOf(s) != -1 || loc.area.indexOf(s) != -1) {

IPEntry entry= newIPEntry();

entry.country=loc.country;

entry.area=loc.area;//得到起始IP

readIP(offset - 4, b4);

entry.beginIp=Utils.getIpStringFromBytes(b4);//得到结束IP

readIP(temp, b4);

entry.endIp=Utils.getIpStringFromBytes(b4);//添加该记录

ret.add(entry);

}

}

}returnret;

}/*** 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录

*

*@params

* 地点子串

*@return包含IPEntry类型的List*/

publicList getIPEntries(String s) {

List ret= newArrayList();try{//映射IP信息文件到内存中

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);//判断是否这个地点里面包含了s子串,如果包含了,添加这个记录到List中,如果没有,继续

if (loc.country.indexOf(s) != -1

|| loc.area.indexOf(s) != -1) {

IPEntry entry= newIPEntry();

entry.country=loc.country;

entry.area=loc.area;//得到起始IP

readIP(offset - 4, b4);

entry.beginIp=Utils.getIpStringFromBytes(b4);//得到结束IP

readIP(temp, b4);

entry.endIp=Utils.getIpStringFromBytes(b4);//添加该记录

ret.add(entry);

}

}

}

}catch(IOException e) {//System.out.println(e.getMessage());

}returnret;

}/*** 从内存映射文件的offset位置开始的3个字节读取一个int

*

*@paramoffset

*@return

*/

private int readInt3(intoffset) {

mbb.position(offset);return mbb.getInt() & 0x00FFFFFF;

}/*** 从内存映射文件的当前位置开始的3个字节读取一个int

*

*@return

*/

private intreadInt3() {return mbb.getInt() & 0x00FFFFFF;

}/*** 根据IP得到国家名

*

*@paramip

* ip的字节数组形式

*@return国家名字符串*/

public String getCountry(byte[] ip) {//检查ip地址文件是否正常

if (ipFile == null)return "错误的IP数据库文件";//保存ip,转换ip字节数组为字符串形式

String ipStr =Utils.getIpStringFromBytes(ip);//先检查cache中是否已经包含有这个ip的结果,没有再搜索文件

if(ipCache.containsKey(ipStr)) {

IPLocation loc=(IPLocation) ipCache.get(ipStr);returnloc.country;

}else{

IPLocation loc=getIPLocation(ip);

ipCache.put(ipStr, loc.getCopy());returnloc.country;

}

}/*** 根据IP得到国家名

*

*@paramip

* IP的字符串形式

*@return国家名字符串*/

publicString getCountry(String ip) {returngetCountry(Utils.getIpByteArrayFromString(ip));

}/*** 根据IP得到地区名

*

*@paramip

* ip的字节数组形式

*@return地区名字符串*/

public String getArea(byte[] ip) {//检查ip地址文件是否正常

if (ipFile == null)return "错误的IP数据库文件";//保存ip,转换ip字节数组为字符串形式

String ipStr =Utils.getIpStringFromBytes(ip);//先检查cache中是否已经包含有这个ip的结果,没有再搜索文件

if(ipCache.containsKey(ipStr)) {

IPLocation loc=(IPLocation) ipCache.get(ipStr);returnloc.area;

}else{

IPLocation loc=getIPLocation(ip);

ipCache.put(ipStr, loc.getCopy());returnloc.area;

}

}/*** 根据IP得到地区名

*

*@paramip

* IP的字符串形式

*@return地区名字符串*/

publicString getArea(String ip) {returngetArea(Utils.getIpByteArrayFromString(ip));

}/*** 根据ip搜索ip信息文件,得到IPLocation结构,所搜索的ip参数从类成员ip中得到

*

*@paramip

* 要查询的IP

*@returnIPLocation结构*/

private IPLocation getIPLocation(byte[] ip) {

IPLocation info= null;long offset =locateIP(ip);if (offset != -1)

info=getIPLocation(offset);if (info == null) {

info= newIPLocation();

info.country= "未知国家";

info.area= "未知地区";

}returninfo;

}/*** 从offset位置读取4个字节为一个long,因为java为big-endian格式,所以没办法 用了这么一个函数来做转换

*

*@paramoffset

*@return读取的long值,返回-1表示读取文件失败*/

private long readLong4(longoffset) {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);returnret;

}catch(IOException e) {return -1;

}

}/*** 从offset位置读取3个字节为一个long,因为java为big-endian格式,所以没办法 用了这么一个函数来做转换

*

*@paramoffset

*@return读取的long值,返回-1表示读取文件失败*/

private long readLong3(longoffset) {long ret = 0;try{

ipFile.seek(offset);

ipFile.readFully(b3);

ret|= (b3[0] & 0xFF);

ret|= ((b3[1] << 8) & 0xFF00);

ret|= ((b3[2] << 16) & 0xFF0000);returnret;

}catch(IOException e) {return -1;

}

}/*** 从当前位置读取3个字节转换成long

*

*@return

*/

private longreadLong3() {long ret = 0;try{

ipFile.readFully(b3);

ret|= (b3[0] & 0xFF);

ret|= ((b3[1] << 8) & 0xFF00);

ret|= ((b3[2] << 16) & 0xFF0000);returnret;

}catch(IOException e) {return -1;

}

}/*** 从offset位置读取四个字节的ip地址放入ip数组中,读取后的ip为big-endian格式,但是

* 文件中是little-endian形式,将会进行转换

*

*@paramoffset

*@paramip*/

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());

}

}/*** 从offset位置读取四个字节的ip地址放入ip数组中,读取后的ip为big-endian格式,但是

* 文件中是little-endian形式,将会进行转换

*

*@paramoffset

*@paramip*/

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;

}/*** 把类成员ip和beginIp比较,注意这个beginIp是big-endian的

*

*@paramip

* 要查询的IP

*@parambeginIp

* 和被查询IP相比较的IP

*@return相等返回0,ip大于beginIp则返回1,小于返回-1。*/

private int compareIP(byte[] ip, byte[] beginIp) {for (int i = 0; i < 4; i++) {int r =compareByte(ip[i], beginIp[i]);if (r != 0)returnr;

}return 0;

}/*** 把两个byte当作无符号数进行比较

*

*@paramb1

*@paramb2

*@return若b1大于b2则返回1,相等返回0,小于返回-1*/

private int compareByte(byte b1, byteb2) {if ((b1 & 0xFF) > (b2 & 0xFF)) //比较是否大于

return 1;else if ((b1 ^ b2) == 0)//判断是否相等

return 0;else

return -1;

}/*** 这个方法将根据ip的内容,定位到包含这个ip国家地区的记录处,返回一个绝对偏移 方法使用二分法查找。

*

*@paramip

* 要查询的IP

*@return如果找到了,返回结束IP的偏移,如果没有找到,返回-1*/

private long locateIP(byte[] ip) {long m = 0;intr;//比较第一个ip项

readIP(ipBegin, b4);

r=compareIP(ip, b4);if (r == 0)returnipBegin;else if (r < 0)return -1;//开始二分搜索

for (long i = ipBegin, j = ipEnd; i

m=getMiddleOffset(i, j);

readIP(m, b4);

r=compareIP(ip, b4);//log.debug(Utils.getIpStringFromBytes(b));

if (r > 0)

i=m;else if (r < 0) {if (m ==j) {

j-=IP_RECORD_LENGTH;

m=j;

}elsej=m;

}else

return readLong3(m + 4);

}//如果循环结束了,那么i和j必定是相等的,这个记录为最可能的记录,但是并非//肯定就是,还要检查一下,如果是,就返回结束地址区的绝对偏移

m = readLong3(m + 4);

readIP(m, b4);

r=compareIP(ip, b4);if (r <= 0)returnm;else

return -1;

}/*** 得到begin偏移和end偏移中间位置记录的偏移

*

*@parambegin

*@paramend

*@return

*/

private long getMiddleOffset(long begin, longend) {long records = (end - begin) /IP_RECORD_LENGTH;

records>>= 1;if (records == 0)

records= 1;return begin + records *IP_RECORD_LENGTH;

}/*** 给定一个ip国家地区记录的偏移,返回一个IPLocation结构

*

*@paramoffset

*@return

*/

private IPLocation getIPLocation(longoffset) {try{//跳过4字节ip

ipFile.seek(offset + 4);//读取第一个字节判断是否标志字节

byte b =ipFile.readByte();if (b ==AREA_FOLLOWED) {//读取国家偏移

long countryOffset =readLong3();//跳转至偏移处

ipFile.seek(countryOffset);//再检查一次标志字节,因为这个时候这个地方仍然可能是个重定向

b =ipFile.readByte();if (b ==NO_AREA) {

loc.country=readString(readLong3());

ipFile.seek(countryOffset+ 4);

}elseloc.country=readString(countryOffset);//读取地区标志

loc.area =readArea(ipFile.getFilePointer());

}else if (b ==NO_AREA) {

loc.country=readString(readLong3());

loc.area= readArea(offset + 8);

}else{

loc.country= readString(ipFile.getFilePointer() - 1);

loc.area=readArea(ipFile.getFilePointer());

}returnloc;

}catch(IOException e) {return null;

}

}/***@paramoffset

*@return

*/

private IPLocation getIPLocation(intoffset) {//跳过4字节ip

mbb.position(offset + 4);//读取第一个字节判断是否标志字节

byte b =mbb.get();if (b ==AREA_FOLLOWED) {//读取国家偏移

int countryOffset =readInt3();//跳转至偏移处

mbb.position(countryOffset);//再检查一次标志字节,因为这个时候这个地方仍然可能是个重定向

b =mbb.get();if (b ==NO_AREA) {

loc.country=readString(readInt3());

mbb.position(countryOffset+ 4);

}elseloc.country=readString(countryOffset);//读取地区标志

loc.area =readArea(mbb.position());

}else if (b ==NO_AREA) {

loc.country=readString(readInt3());

loc.area= readArea(offset + 8);

}else{

loc.country= readString(mbb.position() - 1);

loc.area=readArea(mbb.position());

}returnloc;

}/*** 从offset偏移开始解析后面的字节,读出一个地区名

*

*@paramoffset

*@return地区名字符串

*@throwsIOException*/

private String readArea(long offset) throwsIOException {

ipFile.seek(offset);byte b =ipFile.readByte();if (b == 0x01 || b == 0x02) {long areaOffset = readLong3(offset + 1);if (areaOffset == 0)return "未知地区";else

returnreadString(areaOffset);

}else

returnreadString(offset);

}/***@paramoffset

*@return

*/

private String readArea(intoffset) {

mbb.position(offset);byte b =mbb.get();if (b == 0x01 || b == 0x02) {int areaOffset =readInt3();if (areaOffset == 0)return "未知地区";else

returnreadString(areaOffset);

}else

returnreadString(offset);

}/*** 从offset偏移处读取一个以0结束的字符串

*

*@paramoffset

*@return读取的字符串,出错返回空字符串*/

private String readString(longoffset) {try{

ipFile.seek(offset);inti;for (i = 0, buf[i] = ipFile.readByte(); buf[i] != 0; buf[++i] =ipFile

.readByte())

;if (i != 0)return Utils.getString(buf, 0, i, "GBK");

}catch(IOException e) {//System.out.println(e.getMessage());

}return "";

}/*** 从内存映射文件的offset位置得到一个0结尾字符串

*

*@paramoffset

*@return

*/

private String readString(intoffset) {try{

mbb.position(offset);inti;for (i = 0, buf[i] = mbb.get(); buf[i] != 0; buf[++i] =mbb.get())

;if (i != 0)return Utils.getString(buf, 0, i, "GBK");

}catch(IllegalArgumentException e) {//System.out.println(e.getMessage());

}return "";

}publicString getAddress(String ip) {

String country= getCountry(ip).equals(" CZ88.NET") ? "null": getCountry(ip);

String area= getArea(ip).equals(" CZ88.NET") ? "null": getArea(ip);

String address= country + "|" +area;returnaddress.trim();

}public static voidmain(String[] args) {

IPSeeker is= IPSeeker.getInstance("conf"+File.separator+"QQwry.dat");

System.out.println(is.getAddress(args[0]));

System.out.println(is.getArea(args[0]));

System.out.println(is.getCountry(args[0]));

}

}/*** * 一条IP范围记录,不仅包括国家和区域,也包括起始IP和结束IP *

*@authorswallow*/

classIPEntry {publicString beginIp;publicString endIp;publicString country;publicString area;/*** 构造函数*/

publicIPEntry() {

beginIp= endIp = country = area = "";

}publicString toString() {return this.area + " " + this.country + "IP范围:" + this.beginIp + "-"

+ this.endIp;

}

}/***@authorLJ-silver*/

classUtils {/*** 从ip的字符串形式得到字节数组形式

*

*@paramip

* 字符串形式的ip

*@return字节数组形式的ip*/

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());

}returnret;

}/*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));

}*/

/*** 对原始字符串进行编码转换,如果失败,返回原始的字符串

*

*@params

* 原始字符串

*@paramsrcEncoding

* 源编码方式

*@paramdestEncoding

* 目标编码方式

*@return转换编码后的字符串,失败返回原始字符串*/

public staticString getString(String s, String srcEncoding,

String destEncoding) {try{return newString(s.getBytes(srcEncoding), destEncoding);

}catch(UnsupportedEncodingException e) {returns;

}

}/*** 根据某种编码方式将字节数组转换成字符串

*

*@paramb

* 字节数组

*@paramencoding

* 编码方式

*@return如果encoding不支持,返回一个缺省编码的字符串*/

public static String getString(byte[] b, String encoding) {try{return newString(b, encoding);

}catch(UnsupportedEncodingException e) {return newString(b);

}

}/*** 根据某种编码方式将字节数组转换成字符串

*

*@paramb

* 字节数组

*@paramoffset

* 要转换的起始位置

*@paramlen

* 要转换的长度

*@paramencoding

* 编码方式

*@return如果encoding不支持,返回一个缺省编码的字符串*/

public static String getString(byte[] b, int offset, intlen,

String encoding) {try{return newString(b, offset, len, encoding);

}catch(UnsupportedEncodingException e) {return newString(b, offset, len);

}

}/***@paramip

* ip的字节数组形式

*@return字符串形式的ip*/

public static String getIpStringFromBytes(byte[] ip) {

StringBuffer sb= newStringBuffer();

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);returnsb.toString();

}

}

mysql 纯真ip 导出dat_用纯真ip数据库.dat文件查询ip归属相关推荐

  1. C# 调用IP库(QQWry.Dat)查询IP位置及自动升级IP库方法(附IP库下载地址及相关dll下载)

    前言 C# 用IP地址(123.125.114.144)查询位置(北京市百度公司)的东西,非常好用也非常方便,可手动升级刷新IP库,一次编码永久收益,可支持winform.asp.net等程序. 本文 ...

  2. C# 调用IP库(QQWry.Dat)查询IP位置及自动升级IP库方法(附IP库下载地址及相关dll下载)...

    前言 C# 用IP地址(123.125.114.144)查询位置(北京市百度公司)的东西,非常好用也非常方便,可手动升级刷新IP库,一次编码永久收益,可支持winform.asp.net等程序. 本文 ...

  3. C# 调用IP库(QQWry.Dat)查询IP位置及自动升级IP库方法【转】

    前言 C# 用IP地址(123.125.114.144)查询位置(北京市百度公司)的东西,非常好用也非常方便,可手动升级刷新IP库,一次编码永久收益,可支持winform.asp.net等程序. 本文 ...

  4. mysql 纯真ip 导出dat_C# 读取纯真IP数据库QQWry.dat获取地区信息

    using System; using System.Collections.Generic; using System.Text; using System.IO; namespace 纯真IP数据 ...

  5. linux查看Windows的ip命令,在windows系统和linux系统中查询IP地址命令的不同

    在linux和windows系统上查询IP地址的命令是不一样的. 在linux中的命令行模式下,输入ifconfig即可查询到IP.而在windows系统下要查询IP地址需要先打开dos命令行,然后输 ...

  6. C#读取QQWry.Dat文件实现IP查询

    QQWry数据库下载地址:http://download.csdn.net/detail/fwj380891124/4385554 using System;using System.Collecti ...

  7. mysql本地导入导出_MySQL本地登录及数据库导入导出

    注意:本地MySQL服务要开启 更新整个数据库 1.将正式服务器上的数据库做备份 登录到正式服务器,执行如下命令:(注意空格) mysqldump -uroot –p密码 数据库名 -P 接口 --d ...

  8. python读取遥感 dat_基于python批量处理dat文件及科学计算方法详解

    摘要:主要介绍一些python的文件读取功能,文件内容修改,文件名后缀更改等操作. 批处理文件功能 import os path1 = 'C:\\Users\\awake_ljw\\Documents ...

  9. Shell | 查询IP

    本文参考 1' 2,总结几种在linux系统上查询当前使用PC的IP的方式. Updated: 2022 / 3 / 18 Shell | 查询IP 查询 界面 终端 ifconfig ip host ...

  10. 纯真数据库理解及根据IP定位地区

    在系统中,查看用户的登录信息是一个很常见的功能.我们往往会记录下用户计算机的IP和地理位置,然而IP地址记录非常容易,但是地理位置相对来说较难.开始,菜鸟是想创建一个IP地址库,根据IP地址库查找相应 ...

最新文章

  1. OpenCV向Facemark API添加新算法
  2. c 用mysql导出xls文件怎么打开_mysql——xls表格的导出和导入(重点)
  3. [leetcode]83.删除排序链表中的重复元素
  4. Angular、React、Vue.js 等 6 大主流 Web 框架都有什么优缺点?
  5. 企业微信应用设置可信域名_怎么设置企业微信朋友圈功能?企业微信朋友圈功能有哪些限制?...
  6. CentOS7 网络配置--NAT模式
  7. [转]响应式web设计之CSS3 Media Queries
  8. 最全的java手机游戏免费下载基地,手慢无
  9. 学习笔记(16):GIS遥感科技论文写作技巧与方法初探-地理探测器
  10. Java-JNI调用过程
  11. Mysql相关操作与命令
  12. 学校计算机教室张贴些,计算机教室墙面布置方法是什么?
  13. EasyCVR实时录像接口教程:如何获取国标接入的摄像头设备录像?
  14. How to covert HEIF to JPG with Java
  15. C++ - 多继承方式会产生多个虚函数表
  16. mac 好玩的软件推荐
  17. mysql 密码修改
  18. php手机浏览器获取经纬度,H5移动端调用浏览器Geolocation方法获取手机gps经纬度方法...
  19. HCIE-Cloud Computing LAB备考第二步:实战解题–第五题:论述一--双活容灾设计--记忆宫殿
  20. eclipse实现MySQL分页的类_JavaWeb案例(MVC+MySQL+分页功能)+前后端分离

热门文章

  1. 2021教资高中信息技术笔记知识点48页pdf
  2. JavaScript运算符优先级
  3. SAP-简单的OALV演示练习
  4. access根据所属院系修改学号_2016年计算机二级《Access》冲刺试题及答案
  5. 计算机大学职业规划2000字论文格式,大学生职业生涯规划2000字论文
  6. 计算机专业职业规划范文800字,计算机专业学生职业生涯规划书
  7. ProE/Creo8.0学习笔记(更新中)
  8. 比较万能的匹配邮箱的正则表达式
  9. 基于Python构建土地利用转移矩阵及完成Markov预测
  10. flash实验中需添加的flash.c文件