本文主要是介绍java读取纯真IP数据库QQwry.dat的源代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
java读取纯真IP数据库QQwry.dat的源代码,要运行此程序必须有到网上下载QQwry.da,由于太大,我这里就不提供了。
一、IPEntry.java
/** */ /** *
* 一条IP范围记录,不仅包括国家和区域,也包括起始IP和结束IP *
*
* @author swallow */
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;
}
}
* 一条IP范围记录,不仅包括国家和区域,也包括起始IP和结束IP *
*
* @author swallow */
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;
}
}
/**/ /*
* Created on 2004-8-4
*
*/
import java.io.UnsupportedEncodingException;
/** */ /**
* @author LJ-silver
*/
public class Utils ... {
/** *//**
* 从ip的字符串形式得到字节数组形式
* @param ip 字符串形式的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());
}
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));
}
/** *//**
* 对原始字符串进行编码转换,如果失败,返回原始的字符串
* @param s 原始字符串
* @param srcEncoding 源编码方式
* @param destEncoding 目标编码方式
* @return 转换编码后的字符串,失败返回原始字符串
*/
public static String getString(String s, String srcEncoding, String destEncoding) ...{
try ...{
return new String(s.getBytes(srcEncoding), destEncoding);
} catch (UnsupportedEncodingException e) ...{
return s;
}
}
/** *//**
* 根据某种编码方式将字节数组转换成字符串
* @param b 字节数组
* @param encoding 编码方式
* @return 如果encoding不支持,返回一个缺省编码的字符串
*/
public static String getString(byte[] b, String encoding) ...{
try ...{
return new String(b, encoding);
} catch (UnsupportedEncodingException e) ...{
return new String(b);
}
}
/** *//**
* 根据某种编码方式将字节数组转换成字符串
* @param b 字节数组
* @param offset 要转换的起始位置
* @param len 要转换的长度
* @param encoding 编码方式
* @return 如果encoding不支持,返回一个缺省编码的字符串
*/
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);
}
}
/** *//**
* @param ip ip的字节数组形式
* @return 字符串形式的ip
*/
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();
}
}
* Created on 2004-8-4
*
*/
import java.io.UnsupportedEncodingException;
/** */ /**
* @author LJ-silver
*/
public class Utils ... {
/** *//**
* 从ip的字符串形式得到字节数组形式
* @param ip 字符串形式的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());
}
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));
}
/** *//**
* 对原始字符串进行编码转换,如果失败,返回原始的字符串
* @param s 原始字符串
* @param srcEncoding 源编码方式
* @param destEncoding 目标编码方式
* @return 转换编码后的字符串,失败返回原始字符串
*/
public static String getString(String s, String srcEncoding, String destEncoding) ...{
try ...{
return new String(s.getBytes(srcEncoding), destEncoding);
} catch (UnsupportedEncodingException e) ...{
return s;
}
}
/** *//**
* 根据某种编码方式将字节数组转换成字符串
* @param b 字节数组
* @param encoding 编码方式
* @return 如果encoding不支持,返回一个缺省编码的字符串
*/
public static String getString(byte[] b, String encoding) ...{
try ...{
return new String(b, encoding);
} catch (UnsupportedEncodingException e) ...{
return new String(b);
}
}
/** *//**
* 根据某种编码方式将字节数组转换成字符串
* @param b 字节数组
* @param offset 要转换的起始位置
* @param len 要转换的长度
* @param encoding 编码方式
* @return 如果encoding不支持,返回一个缺省编码的字符串
*/
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);
}
}
/** *//**
* @param ip ip的字节数组形式
* @return 字符串形式的ip
*/
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
*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma < stubma@ 163 .com >
*
* This program is free software; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330 , Boston, MA 02111 - 1307 USA
*/
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
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma < stubma@ 163 .com >
*
* This program is free software; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330 , Boston, MA 02111 - 1307 USA
*/
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读取纯真IP数据库QQwry.dat的源代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!