我获取了别人的IP,想在本地后台显示这IP所有在地区,如果查询IP所在地区 我获取了别人的IP,想在本地后台显示这IP所有在地区,如果查询IP所在地区我就是要精确到所有IP的省份就行,有谁有这样的IP数据库,要用什么方法最好,提供最快,最好的查询,谁能否给点建议和相关的程序代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以用http get或post的方法去一些网站查,比如萍心网之类或者去网上找一些类似纯真ip数据库之类的 以前那个版本的QQ好像有这个数据库,lz可以去找找看 那弄到了IP数据库,是不是要一个一个的 select到数据库里比对查询,会不会好慢啊 我的mail:[email protected]正好我昨天也有这个需求,借LumaQQ源码改了一下现在可以用了,速度很快的,给你一份 package com.ip;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.HashMap;import java.util.List;import java.util.Map;import org.apache.log4j.Logger;/** * <pre> * 关于IP数据库文件格式,请参考LumaQQ主页文档“纯真IP数据库格式详解”一文。 * </pre> * * @author luma */public class IPSeeker { /** * <pre> * 用来封装ip相关信息,目前只有两个字段,ip所在的国家和地区 * </pre> * * @author luma */ 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 int IP_RECORD_LENGTH = 7; private static final byte REDIRECT_MODE_1 = 0x01; private static final byte REDIRECT_MODE_2 = 0x02; // Log对象 private static Logger log = Logger.getLogger(IPSeeker.class); // 用来做为cache,查询一个ip时首先查看cache,以减少不必要的重复查找 private Map<String, IPLocation> ipCache; // 随机文件访问类 private RandomAccessFile ipFile; // 内存映射文件 private MappedByteBuffer mbb; // 单一模式实例 private static IPSeeker instance = new IPSeeker(); // 起始地区的开始和结束的绝对偏移 private long ipBegin, ipEnd; // 为提高效率而采用的临时变量 private IPLocation loc; private byte[] buf; private byte[] b4; private byte[] b3; private String IP_FILE="";//纯真ip库的位置 private static String bad_ip_file; private static String unknown_country; private static String unknown_area; /** * 私有构造函数 */ private IPSeeker() { init(); ipCache = new HashMap<String, IPLocation>(); 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) { log.error("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) { log.error("IP地址信息文件格式有错误,IP显示功能将无法使用"); ipFile = null; } } } /** * @return 单一实例 */ public static IPSeeker getInstance() { return instance; } /** * 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录 * @param s 地点子串 * @return 包含IPEntry类型的List */ public List getIPEntriesDebug(String s) { List<IPEntry> ret = new ArrayList<IPEntry>(); 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 ipLoc = getIPLocation(temp); // 判断是否这个地点里面包含了s子串,如果包含了,添加这个记录到List中,如果没有,继续 if(ipLoc.country.indexOf(s) != -1 || ipLoc.area.indexOf(s) != -1) { IPEntry entry = new IPEntry(); entry.country = ipLoc.country; entry.area = ipLoc.area; // 得到起始IP readIP(offset - 4, b4); entry.beginIp = IPUtil.getIpStringFromBytes(b4); // 得到结束IP readIP(temp, b4); entry.endIp = IPUtil.getIpStringFromBytes(b4); // 添加该记录 ret.add(entry); } } } return ret; } /** * 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录 * @param s 地点子串 * @return 包含IPEntry类型的List */ public List<IPEntry> getIPEntries(String s) { List<IPEntry> ret = new ArrayList<IPEntry>(); 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 ipLoc = getIPLocation(temp); // 判断是否这个地点里面包含了s子串,如果包含了,添加这个记录到List中,如果没有,继续 if(ipLoc.country.indexOf(s) != -1 || ipLoc.area.indexOf(s) != -1) { IPEntry entry = new IPEntry(); entry.country = ipLoc.country; entry.area = ipLoc.area; // 得到起始IP readIP(offset - 4, b4); entry.beginIp = IPUtil.getIpStringFromBytes(b4); // 得到结束IP readIP(temp, b4); entry.endIp = IPUtil.getIpStringFromBytes(b4); // 添加该记录 ret.add(entry); } } } } catch (IOException e) { log.error(e.getMessage()); } return ret; } 看不明白,我刚学java,你是把IP的数据库,先读到内存吗?你的思路是什么,我现在是找到了一个IP数据库,一个一个IP,通过数据库select 来查询到地区后,就再插入地另一个IP来源历史表里面,有什么更好的方法,你的QQ是多少啊,我的QQ是:81832527,能否加加我, jsp页面有大网站使用吗? 十万火急!struts+hibernate验证问题 struts2核心包不能被jboss加载 关于HQL的考试题 求助!别的都可以,唯独它..... 问一些关于学习水晶报表开发的方法 请问大家用java开发web项目时,常用的邮件系统有哪些 IE的缓存问题:程序中的错误 问个数据库的问题!!! 关于UML类图的问题,想请教前辈们 [菜鸟提问]关于从oracle数据库中将blob数据输出到jsp上的问题,急! 查询一个如何打印出从A网站,把B网站通过窗口弹出来,让B网站知道是A网站地址弹出来的
或者去网上找一些类似纯真ip数据库之类的
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.HashMap;
import java.util.List;
import java.util.Map;import org.apache.log4j.Logger;
/**
* <pre>
* 关于IP数据库文件格式,请参考LumaQQ主页文档“纯真IP数据库格式详解”一文。
* </pre>
*
* @author luma
*/
public class IPSeeker {
/**
* <pre>
* 用来封装ip相关信息,目前只有两个字段,ip所在的国家和地区
* </pre>
*
* @author luma
*/
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 int IP_RECORD_LENGTH = 7;
private static final byte REDIRECT_MODE_1 = 0x01;
private static final byte REDIRECT_MODE_2 = 0x02; // Log对象
private static Logger log = Logger.getLogger(IPSeeker.class);
// 用来做为cache,查询一个ip时首先查看cache,以减少不必要的重复查找
private Map<String, IPLocation> ipCache;
// 随机文件访问类
private RandomAccessFile ipFile;
// 内存映射文件
private MappedByteBuffer mbb;
// 单一模式实例
private static IPSeeker instance = new IPSeeker();
// 起始地区的开始和结束的绝对偏移
private long ipBegin, ipEnd;
// 为提高效率而采用的临时变量
private IPLocation loc;
private byte[] buf;
private byte[] b4;
private byte[] b3;
private String IP_FILE="";//纯真ip库的位置
private static String bad_ip_file;
private static String unknown_country;
private static String unknown_area; /**
* 私有构造函数
*/
private IPSeeker() {
init();
ipCache = new HashMap<String, IPLocation>();
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) {
log.error("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) {
log.error("IP地址信息文件格式有错误,IP显示功能将无法使用");
ipFile = null;
}
}
}
* @return 单一实例
*/
public static IPSeeker getInstance() {
return instance;
} /**
* 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录
* @param s 地点子串
* @return 包含IPEntry类型的List
*/
public List getIPEntriesDebug(String s) {
List<IPEntry> ret = new ArrayList<IPEntry>();
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 ipLoc = getIPLocation(temp);
// 判断是否这个地点里面包含了s子串,如果包含了,添加这个记录到List中,如果没有,继续
if(ipLoc.country.indexOf(s) != -1 || ipLoc.area.indexOf(s) != -1) {
IPEntry entry = new IPEntry();
entry.country = ipLoc.country;
entry.area = ipLoc.area;
// 得到起始IP
readIP(offset - 4, b4);
entry.beginIp = IPUtil.getIpStringFromBytes(b4);
// 得到结束IP
readIP(temp, b4);
entry.endIp = IPUtil.getIpStringFromBytes(b4);
// 添加该记录
ret.add(entry);
}
}
}
return ret;
} /**
* 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录
* @param s 地点子串
* @return 包含IPEntry类型的List
*/
public List<IPEntry> getIPEntries(String s) {
List<IPEntry> ret = new ArrayList<IPEntry>();
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 ipLoc = getIPLocation(temp);
// 判断是否这个地点里面包含了s子串,如果包含了,添加这个记录到List中,如果没有,继续
if(ipLoc.country.indexOf(s) != -1 || ipLoc.area.indexOf(s) != -1) {
IPEntry entry = new IPEntry();
entry.country = ipLoc.country;
entry.area = ipLoc.area;
// 得到起始IP
readIP(offset - 4, b4);
entry.beginIp = IPUtil.getIpStringFromBytes(b4);
// 得到结束IP
readIP(temp, b4);
entry.endIp = IPUtil.getIpStringFromBytes(b4);
// 添加该记录
ret.add(entry);
}
}
}
} catch (IOException e) {
log.error(e.getMessage());
}
return ret;
}