日期:2014-05-20 浏览次数:21061 次
严重: Servlet.service() for servlet action threw exception java.lang.NullPointerException at IPAdress.IPSeeker.<init>(IPSeeker.java:101) at IPAdress.IPSeeker.<clinit>(IPSeeker.java:80)
package IPAdress; 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; public class IPSeeker{ /** *//** * * 用来封装ip相关信息,目前只有两个字段,ip所在的国家和地区 * * * @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); // 一些固定常量,比如记录长度等等 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,以减少不必要的重复查找 private Hashtable 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; /** *//** * 私有构造函数 */ public 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显示功能将无法使用"); 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; } } } /** *//** * @return 单一实例 */ public static IPSeeker getInstance(){ return instance; } /** *//** * 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录 * @param s 地点子串 * @return 包含IPEntry类型的List */ public List getIPEntriesDebug(String s){ List ret = new ArrayList(); 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 = new IPEntry(); 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); } } } return ret; } /** *//** * 给定一个地点的不完全名字,得到一系列包含s子串的IP范围记录 * @param s 地点子串 * @return 包含IPEntry类型的List */ public List getIPEntries(String s){ List ret = new ArrayList(); 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 = new IPEntry(); 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()); } return ret; } 代码太多,中间省略。。 ................ 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 String getipAddress(String ip){ String country = getCountry(ip).equals(" CZ88.NET")?"":getCountry(ip); //String area = getArea(ip).equals(" CZ88.NET")?"":getArea(ip); String address = country; return address.trim(); } }