2023年3月22日发(作者:永久雷诺)ip查询归属地收藏
import .*;
import .*;
import der;
import ByteBuffer;
import annel;
public class IpAddress {
//数据库地址
private String dataPath = "";
//随机文件访问类
private RandomAccessFile ipFile = null;
//单一模式实例
private static IpAddress instance = new IpAddress();
//ip开始结束位置
private long ipBegin=0L;
private long ipEnd=0L;
//ip总数
private long ipSum=0L;
//国家,地区
private String country="";
private String area="";
// 一些固定常量,比如记录长度等等
private static final int RECORD_LENGTH = 7;
private static final xxx88
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) {
n("IP地址信息文件没有找到,IP显示功能将无法使用");
}
if(ipFile != null) {
try {
ipBegin = byteArrayToLong(readBytes(0,4));
ipEnd = byteArrayToLong(readBytes(4,4));
if(ipBegin == -1 || ipEnd == -1) {
();
ipFile = null;
}
} catch (IOException e) {
n("IP地址信息文件格式有错误,IP显示功能将无法使用");
}
}
ipSum = (ipEnd-ipBegin)/RECORD_LENGTH+1;
}
/**
* 在指定位置读取一定数目的字节
* @param offt 位置
* @param num 多少个字节
* @return ret
*/
private byte[] readBytes(long offt, int num) {
byte[] ret = new byte[num];
try {
(offt);
for(int i=0; i != num; i++) {
ret[i] = te();
}
return ret;
} catch (IOException e) {
n("读取文件失败_readBytes");
return ret;
}
}
/**
* 当前位置读取一定数目的字节
* @param num 多少个字节
* @return ret
*/
private byte[] readBytes(int num) {
byte[] ret = new byte[num];
try {
for(int i=0; i != num; i++) {
ret[i] = te();
}
return ret;
} catch (IOException e) {
n("读取文件失败_readBytes");
return ret;
}
}
/**
* 对little-endian字节序进行了转换
* byte[]转换为long
* @param b
* @return ret
*/
private long byteArrayToLong(byte[] b) {
long ret = 0;
for(int i=0; i<
h; i++) {
ret |= ( b[i] << (0x8*i) & (0xFF * (long)((0x100,i))) );
}
return ret;
}
/**
* 对little-endian字节序进行了转换
* @param ip ip的字节数组形式
* @return ip的字符串形式
*/
private String byteArrayToStringIp(byte[] ip) {
StringBuffer sb = new StringBuffer();
for(int i=-1; i>=0; i--) {
(ip[i] & 0xFF);
(".");
}
CharAt(()-1);
return ng下拉选择
();
}
/**
* 把ip字符串转换为long型
* @param ip
* @return long
*/
private long StingIpToLong(String ip) {
String[] arr = (".");
return (f(arr[0])*0x1000000 +
f(arr[1])*0x10000 +
f(arr[2])*0x100 +
f(arr[3]));
}
/**
* 搜索ip,二分法
* @param String ip字符串0.0.0.0到255.255.255.255
* @return long ip所在位置
*/
public long ekIp(String ip) {
long tmp = StingIpToLong(ip);
long i=0;
long j=ipSum;
long m = 0;
long lm=0L;
while(i
m = (i+j)/2;
lm = m*RECORD_LENGTH + ipBegin;
if( tmp == byteArrayToLong(readBytes(lm, 4))){
return byteArrayToLong(readBytes(3));
}el if(j==(i+1)) {
return byteArrayToLong(readBytes(3));
}el if( tmp > byteArrayToLong(readBytes(lm, 4))){
i = m;
}el/* if( tmp < byteArrayToLong(readBytes(lm, 4)))*/{
j = m;
}
}
n("没有找到ip");
return -1L;
}
private String readArea(long offt) throws IOException {
(offt);
byte b = te();
if(b == 0x01 || b == 0x02) {
long areaOfft =byteArrayToLong(readBytes(offt+1,3));
// if(areaOfft == 0)
// return "未知";
// el
return readString(areaOfft);
} el
return readString(offt);
}
/**
* 通过ip位置获取国家地区,
* 参照纯真ip数据库结构
* @param offt
* @return 国家+地绿色出行作文
区
*/
private String ekCountryArea(long offt) {
try {
(offt + 4);
byte b = te();
if(b == AREA_FOLLOWED) {
long countryOfft = byteArrayToLong(readBytes(3));
(countryOfft);
b = te();
if(b == NO_AREA) {
country = readString(byteArrayToLong(readBytes(3)));
(countryOfft + 4);
} el
country = readString(countryOfft);
area = readArea(ePointer());
} el if(b == NO_AREA) {
country = readString(byteArrayToLong(readBytes(3)));
area = readArea(o初一知识点
fft + 8);
} el {
country = readString(ePointer() - 1);
area = readArea(ePointer());
}
return country+" "+area;
} catch (IOException e) {
return null;
}
}
/**
* 从offt偏移处读取一个以0结束的字符串
* @param offt
* @return ret 读取的字符串,出错返回空字符串
*/
private String readString(long offt){
try {
(offt);
byte[] b = new byte[128];
int i;
for(i=0; ( != i) && ((b[i]=te()) != 0); i++);
String ret = new String(b, 0 , i/*, "GBK"*/);
ret = ();
return (("") ||
f("") != -1 )?"未知":ret;
} catch (IOException e) {
n("读取文件失败_readString");
}
return "";
}
/**
* 包含字符串的ip记录
* @param addr 地址
* @return IpRecord ip记录
*/
public ArrayList stringToIp(String addr) {
ArrayList ret = new ArrayList();
try{
Fi戒酒方法
leChannel fc = nnel();
MappedByteBuffer mbb = (_ONLY, 0, ());
(_ENDIAN);
//上面3代码未使用,内存映射文件功能未写
for(long i = ipBegin+4; i != ipEnd+4; i += RECORD_LENGTH) {
String sca = ekCountryArea(byteArrayToLong(readBytes(i, 3)));
if(f(addr) != -1) {
IpRecord rec = new IpRecord();
s = sca;
p= byteArrayToStringIp(readBytes(i-4,4));
= byteArrayToStringIp(readBytes(i+3,4));
(rec);
}
}
} catch (IOException 搞笑童话故事
e) {
n(sage());
}
return ret;
}
/**
* 封装ip记录,包括开始ip,结束ip和地址
*/
private class IpRecord {
public String beginIp;
public String endIp;
public String address;
public IpRecord() {
beginIp = endIp = address = "";
}
public String toString() {
return beginIp + " - " + endIp + " " + address;
}
}
/**
* @return 单一实例
*/
public static IpAddress getInstance() {
return instance;
}
/**
* @param ip
* @return ret
*/
public Strin
g IpStringToAddress(String ip) {
//这里要添加ip格式判断
//public boolean isIP(Strin ip)
long ipOfft = ekIp(ip);
String ret = ekCountryArea(ipOfft);
return ret;
}
/**
* @return IpSum
*/
public long getIpSum() {
return ipSum;
}
public static void main(String[] args) {
IpAddress ipAddr = tance();
//ip总数
long l = um();
n(l);
//纯真ip数据更新时间
String str = ngToAddress("255.255.255.0");
n(str);
//测试
String strValue = ngToAddress("211.96.28.105");
n(str);
String [] str1=(" ");
String area=str1[0];
String come=str1[1];
int cometype=4;
if(ns("移动"))
{
cometype=1;
}
el if(ns("联通"))
{
cometype=2;
}
el if(ns("电信"))
{
cometype=3;
}
el
{
cometype=4;
}
n(come+" "+cometype+" "+area);
str = ngToAddress("211.136.115.39");
n(str);
str = ngToAddress("188.1.255.255");
n(str);
str = ngToAddress("220.168.59.166");
n(str);
str = ngToAddress("188.67.234.23");
n(str);
/* dress addr = null;
try{
addr = alHost();
}catch(nHostException e){
tackTrace();
}
String ip=tAddress().toString();//获得本机IP
(ip);
String address=tName().toString();//获得本机名称
(address);
str = ngToAddress(ip);
n(str);*/
ArrayList al = ToIp("网吧");
Iterator it = or();
File f = new File("");
try{
if(!()) {
NewFile();
}
BufferedWriter out = new BufferedWriter(
new OutputStreamWrite中国朝代歌
r(
new FileOutputStream(f, true)
)
);
int i=0;
while(t()) {
(().toString());
e();
i++;
}
(new Date().toString());
("总共搜索到 "+i);
();
}catch(IOException e){
tackTrace();
}
}
}