日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

java纯真ip数据库_java实现对纯真IP数据库的查询

發(fā)布時(shí)間:2023/12/3 数据库 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java纯真ip数据库_java实现对纯真IP数据库的查询 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.IP記錄實(shí)體類 package com.guess.tools; /** *

* 一條IP范圍記錄,不僅包括國(guó)家和區(qū)域,也包括起始IP和結(jié)束IP * *

* @author swallow */

public class IPEntry {

public String beginIp;

public String endIp;

public String country;

public String area; /** *//**

* 構(gòu)造函數(shù)

*/ public IPEntry() {

beginIp = endIp = country = area = "";

} public String toString(){

return this.area+" "+this.country+"IP范圍:"+this.beginIp+"-"+this.endIp;

}

}

2.讀取QQwry.dat文件類

package com.guess.tools; 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; /** *//**

* * 用來讀取QQwry.dat文件,以根據(jù)ip獲得好友位置,QQwry.dat的格式是

* 一. 文件頭,共8字節(jié)

* 1. 第一個(gè)起始IP的絕對(duì)偏移, 4字節(jié)

* 2. 最后一個(gè)起始IP的絕對(duì)偏移, 4字節(jié)

* 二. "結(jié)束地址/國(guó)家/區(qū)域"記錄區(qū)

* 四字節(jié)ip地址后跟的每一條記錄分成兩個(gè)部分

* 1. 國(guó)家記錄

* 2. 地區(qū)記錄

* 但是地區(qū)記錄是不一定有的。而且國(guó)家記錄和地區(qū)記錄都有兩種形式

* 1. 以0結(jié)束的字符串

* 2. 4個(gè)字節(jié),一個(gè)字節(jié)可能為0x1或0x2

* a. 為0x1時(shí),表示在絕對(duì)偏移后還跟著一個(gè)區(qū)域的記錄,注意是絕對(duì)偏移之后,而不是這四個(gè)字節(jié)之后

* b. 為0x2時(shí),表示在絕對(duì)偏移后沒有區(qū)域記錄

* 不管為0x1還是0x2,后三個(gè)字節(jié)都是實(shí)際國(guó)家名的文件內(nèi)絕對(duì)偏移

* 如果是地區(qū)記錄,0x1和0x2的含義不明,但是如果出現(xiàn)這兩個(gè)字節(jié),也肯定是跟著3個(gè)字節(jié)偏移,如果不是

* 則為0結(jié)尾字符串

* 三. "起始地址/結(jié)束地址偏移"記錄區(qū)

* 1. 每條記錄7字節(jié),按照起始地址從小到大排列

* a. 起始IP地址,4字節(jié)

* b. 結(jié)束ip地址的絕對(duì)偏移,3字節(jié)

*

* 注意,這個(gè)文件里的ip地址和所有的偏移量均采用little-endian格式,而java是采用

* big-endian格式的,要注意轉(zhuǎn)換

*

*

* @author 馬若劼

*/

public class IPSeeker {

/** *//**

* * 用來封裝ip相關(guān)信息,目前只有兩個(gè)字段,ip所在的國(guó)家和地區(qū)

*

*

* @author swallow */

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); // 一些固定常量,比如記錄長(zhǎng)度等等

private static final int IP_RECORD_LENGTH = 7;

private static final byte AREA_FOLLOWED = 0x01;

private static final byte NO_AREA = 0x2; // 用來做為cache,查詢一個(gè)ip時(shí)首先查看cache,以減少不必要的重復(fù)查找

private Hashtable ipCache;

// 隨機(jī)文件訪問類

private RandomAccessFile ipFile;

// 內(nèi)存映射文件

private MappedByteBuffer mbb;

// 單一模式實(shí)例

private static IPSeeker instance = new IPSeeker();

// 起始地區(qū)的開始和結(jié)束的絕對(duì)偏移

private long ipBegin, ipEnd;

// 為提高效率而采用的臨時(shí)變量

private IPLocation loc;

private byte[] buf;

private byte[] b4;

private byte[] b3; /** *//**

* 私有構(gòu)造函數(shù)

*/

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顯示功能將無(wú)法使用");

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地址信息文件格式有錯(cuò)誤,IP顯示功能將無(wú)法使用");

ipFile = null;

}

}

} /** *//**

* @return 單一實(shí)例

*/

public static IPSeeker getInstance() {

return instance;

} /** *//**

* 給定一個(gè)地點(diǎn)的不完全名字,得到一系列包含s子串的IP范圍記錄

* @param s 地點(diǎn)子串

* @return 包含IPEntry類型的List

*/

public List getIPEntriesDebug(String s) {

List ret = new ArrayList();

long endOffset = ipEnd + 4;

for(long offset = ipBegin + 4; offset (b2 & 0xFF)) // 比較是否大于

return 1;

else if((b1 ^ b2) == 0)// 判斷是否相等

return 0;

else

return -1;

} /** *//**

* 這個(gè)方法將根據(jù)ip的內(nèi)容,定位到包含這個(gè)ip國(guó)家地區(qū)的記錄處,返回一個(gè)絕對(duì)偏移

* 方法使用二分法查找。

* @param ip 要查詢的IP

* @return 如果找到了,返回結(jié)束IP的偏移,如果沒有找到,返回-1

*/

private long locateIP(byte[] ip) {

long m = 0;

int r;

// 比較第一個(gè)ip項(xiàng)

readIP(ipBegin, b4);

r = compareIP(ip, b4);

if(r == 0) return ipBegin;

else if(r 0)

i = m;

else if(r >= 1;

if(records == 0) records = 1;

return begin + records * IP_RECORD_LENGTH;

} /** *//**

* 給定一個(gè)ip國(guó)家地區(qū)記錄的偏移,返回一個(gè)IPLocation結(jié)構(gòu)

* @param offset

* @return

*/

private IPLocation getIPLocation(long offset) {

try {

// 跳過4字節(jié)ip

ipFile.seek(offset + 4);

// 讀取第一個(gè)字節(jié)判斷是否標(biāo)志字節(jié)

byte b = ipFile.readByte();

if(b == AREA_FOLLOWED) {

// 讀取國(guó)家偏移

long countryOffset = readLong3();

// 跳轉(zhuǎn)至偏移處

ipFile.seek(countryOffset);

// 再檢查一次標(biāo)志字節(jié),因?yàn)檫@個(gè)時(shí)候這個(gè)地方仍然可能是個(gè)重定向

b = ipFile.readByte();

if(b == NO_AREA) {

loc.country = readString(readLong3());

ipFile.seek(countryOffset + 4);

} else

loc.country = readString(countryOffset);

// 讀取地區(qū)標(biāo)志

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

}

return loc;

} catch (IOException e) {

return null;

}

} /** *//**

* @param offset

* @return

*/

private IPLocation getIPLocation(int offset) {

// 跳過4字節(jié)ip

mbb.position(offset + 4);

// 讀取第一個(gè)字節(jié)判斷是否標(biāo)志字節(jié)

byte b = mbb.get();

if(b == AREA_FOLLOWED) {

// 讀取國(guó)家偏移

int countryOffset = readInt3();

// 跳轉(zhuǎn)至偏移處

mbb.position(countryOffset);

// 再檢查一次標(biāo)志字節(jié),因?yàn)檫@個(gè)時(shí)候這個(gè)地方仍然可能是個(gè)重定向

b = mbb.get();

if(b == NO_AREA) {

loc.country = readString(readInt3());

mbb.position(countryOffset + 4);

} else

loc.country = readString(countryOffset);

// 讀取地區(qū)標(biāo)志

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

}

return loc;

} /** *//**

* 從offset偏移開始解析后面的字節(jié),讀出一個(gè)地區(qū)名

* @param offset

* @return 地區(qū)名字符串

* @throws IOException

*/

private String readArea(long offset) throws IOException {

ipFile.seek(offset);

byte b = ipFile.readByte();

if(b == 0x01 || b == 0x02) {

long areaOffset = readLong3(offset + 1);

if(areaOffset == 0)

return "未知地區(qū)";

else

return readString(areaOffset);

} else

return readString(offset);

} /** *//**

* @param offset

* @return

*/

private String readArea(int offset) {

mbb.position(offset);

byte b = mbb.get();

if(b == 0x01 || b == 0x02) {

int areaOffset = readInt3();

if(areaOffset == 0)

return "未知地區(qū)";

else

return readString(areaOffset);

} else

return readString(offset);

} /** *//**

* 從offset偏移處讀取一個(gè)以0結(jié)束的字符串

* @param offset

* @return 讀取的字符串,出錯(cuò)返回空字符串

*/

private String readString(long offset) {

try {

ipFile.seek(offset);

int i;

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 "";

} /** *//**

* 從內(nèi)存映射文件的offset位置得到一個(gè)0結(jié)尾字符串

* @param offset

* @return

*/

private String readString(int offset) {

try {

mbb.position(offset);

int i;

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 "";

} public String getAddress(String ip){

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

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

String address = country+" "+area;

return address.trim();

}

public static void main(String args[]) {

List list=IPSeeker.getInstance().getIPEntriesDebug("浙江省杭州市"); }

}

分享到:

2009-11-04 18:57

瀏覽 872

評(píng)論

總結(jié)

以上是生活随笔為你收集整理的java纯真ip数据库_java实现对纯真IP数据库的查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。