IP定位:通过qqwry.bat IP库获取指定IP的地理位置信息

qqwry.bat 下载地址 http://download.csdn.net/detail/takeurhand/8048487

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Name IpAddress
 * @Description 根据Ip判断地理位置
 * @Attention NULL
 * @Tags 
 * @Author CaiYj
 * <br>2014-10-17
 */
public class IpAddress {

   /**
    * bat文件地址(这是一个ip库)
    */
   private String dataPath = "src/QQWry.dat";
   private RandomAccessFile ipFile = null;
   private static IpAddress instance = new IpAddress();
   private long ipBegin=0L;
   private long ipEnd=0L;
   private long ipSum=0L;
   private String country="";
   private static final int RECORD_LENGTH = 7;
   private static final 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) {  
    	   e.printStackTrace();
       }
       if(ipFile != null) {
           try {               
               ipBegin = byteArrayToLong(readBytes(0,4));
               ipEnd = byteArrayToLong(readBytes(4,4));
               if(ipBegin == -1 || ipEnd == -1) {
                   ipFile.close();
                   ipFile = null;
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       ipSum = (ipEnd-ipBegin)/RECORD_LENGTH+1;
   }
   private byte[] readBytes(long offset, int num) {
       byte[] ret = new byte[num];
       try {
           ipFile.seek(offset);

           for(int i=0; i != num; i++) {
               ret[i] = ipFile.readByte();
           }
           return ret;
       } catch (IOException e) {
    	   e.printStackTrace();
           return ret;
       }
   }
   private byte[] readBytes(int num) {
       byte[] ret = new byte[num];
       try {
           for(int i=0; i != num; i++) {
               ret[i] = ipFile.readByte();
           }
           return ret;
       } catch (IOException e) {
           return ret;
       }
   }
   private long byteArrayToLong(byte[] b) {
       long ret = 0;
       for(int i=0; i<b.length; i++) {
           ret |= ( b[i] << (0x8*i) & (0xFF * (long)(Math.pow(0x100,i))) );
       }
       return ret;
   }

   /**
    * @Name 
    * @Description 把ip字符串转换为long型
    * @Attention NULL
    * @ReturnType long NULL
    * @Tags @param ip
    * @Tags @return
    * @Author CaiYj
    * <br>2014-10-17
    */
   private long StingIpToLong(String ip) {
       String[] arr = ip.split("\\.");
       return (Long.valueOf(arr[0])*0x1000000 +
               Long.valueOf(arr[1])*0x10000 +
               Long.valueOf(arr[2])*0x100 +
               Long.valueOf(arr[3]));   
   }
   /**
    * @Name 
    * @Description 搜索ip
    * @Attention NULL
    * @ReturnType long NULL
    * @Tags @param ip
    * @Tags @return
    * @Author CaiYj
    * <br>2014-10-17
    */
   public long seekIp(String ip) {
       long tmp = StingIpToLong(ip);
       long i=0;
       long j=ipSum;
       long m = 0;
       long lm=0L;
       while(i<j) {
           m = (i+j)/2;
           lm = m*RECORD_LENGTH + ipBegin;
           if( tmp == byteArrayToLong(readBytes(lm, 4))){       
               return byteArrayToLong(readBytes(3));
           }else if(j==(i+1)) {
               return byteArrayToLong(readBytes(3));
           }else if( tmp > byteArrayToLong(readBytes(lm, 4))){
               i = m;
           }else/* if( tmp < byteArrayToLong(readBytes(lm, 4)))*/{
               j = m;
           }
       }
       return -1L;
   }
   /**
    * @Name 
    * @Description seekCountryArea di
    * @Attention NULL
    * @ReturnType String NULL
    * @Tags @param offset
    * @Tags @return
    * @Author CaiYj
    * <br>2014-10-17
    */
   private String seekCountryArea(long offset) {
           try {
           ipFile.seek(offset + 4);
           byte b = ipFile.readByte();
           if(b == AREA_FOLLOWED) 
           {
               long countryOffset = byteArrayToLong(readBytes(3));
               ipFile.seek(countryOffset);
               b = ipFile.readByte();
               if(b == NO_AREA) {
                   country = readString(byteArrayToLong(readBytes(3)));
                   ipFile.seek(countryOffset + 4);
               } else
                   country = readString(countryOffset);
               //area = readArea(ipFile.getFilePointer());
           } else if(b == NO_AREA) {
               country = readString(byteArrayToLong(readBytes(3)));
              // area = readArea(offset + 8);       
           } else {
               country = readString(ipFile.getFilePointer() - 1);
               //area = readArea(ipFile.getFilePointer());
           }
           return readText(country,"省(.+?)市");//+" "+area;
       } catch (IOException e) {
           return null;
       }
   }
   
  /**
   * @Name 
   * @Description readText
   * @Attention NULL
   * @ReturnType String NULL
   * @Tags @param result
   * @Tags @param identifier
   * @Tags @return
   * @Author CaiYj
   * <br>2014-10-17
   */
   public static String readText(String result, String identifier) {   
       Pattern shopNumberPattern = Pattern.compile(identifier);   
       Matcher shopNamMatcher = shopNumberPattern.matcher(result);   
       if (shopNamMatcher.find())   
           return shopNamMatcher.group(1);   
       return "";   
   } 

   /**
    * @Name 
    * @Description 从offset偏移处读取一个以0结束的字符串
    * @Attention NULL
    * @ReturnType String NULL
    * @Tags @param offset
    * @Tags @return
    * @Author CaiYj
    * <br>2014-10-17
    */
   private String readString(long offset){
       try {
           ipFile.seek(offset);
           byte[] b = new byte[128];
           int i;
           for(i=0; (b.length != i) && ((b[i]=ipFile.readByte()) != 0); i++);
           String ret = new String(b, 0 , i/*, "GBK"*/);
           ret = ret.trim();
           return (ret.equals("") ||
                          ret.indexOf("CZ88.NET") != -1 )?"未知":ret;
       } catch (IOException e) {
           System.out.println("读取文件失败_readString");
       }
       return "";
   }

   /**
    * @Name IpRecord
    * @Description 封装ip记录,包括开始ip,结束ip和地址
    * @Attention NULL
    * @Tags 
    * @Author CaiYj
    * <br>2014-10-17
    */
   @SuppressWarnings("unused")
   private class IpRecord {
       public String beginIp;
       public String endIp;
       public String address;

       public IpRecord() {
           beginIp = endIp = address = "";   
       }

       public String toString() {
           return beginIp + " - " + endIp + " " + address;
       }
   }

   /**
    * @Name 
    * @Description Singleton
    * @Attention NULL
    * @ReturnType IpAddress NULL
    * @Tags @return
    * @Author CaiYj
    * <br>2014-10-17
    */
   public static IpAddress getInstance() {
       return instance;
   }
   /**
    * @Name 
    * @Description NULL
    * @Attention NULL
    * @ReturnType String NULL
    * @Tags @param ip
    * @Tags @return
    * @Author CaiYj
    * <br>2014-10-17
    */
   public String IpStringToAddress(String ip) {
       long ipOffset = seekIp(ip);
       String ret = seekCountryArea(ipOffset);
       return ret;
   }

  /**
   * @Name 
   * @Description NULL
   * @Attention NULL
   * @ReturnType long ipSum
   * @Tags @return
   * @Author CaiYj
   * <br>2014-10-17
   */
   public long getIpSum() {
       return ipSum;
   }

   public static void main(String[] args) throws UnknownHostException {
       IpAddress ipAddr = IpAddress.getInstance();
//       	/**
//       	 * ip总数 long
//       	 */
//       long l = ipAddr.getIpSum();
//       System.out.println(l);
       String str = ipAddr.IpStringToAddress("255.255.255.0");
       System.out.println("255.255.255.0 "+str);
       str = ipAddr.IpStringToAddress("113.235.124.128");
       System.out.println("113.235.124.128 "+str);
       str = ipAddr.IpStringToAddress("192.168.0.174");
       System.out.println("192.168.0.174"+str);
       InetAddress inet = InetAddress.getLocalHost(); 
       System.out.println("本地ip" + inet.getHostAddress()); 
   }   
} 



猜你喜欢

转载自blog.csdn.net/takeurhand/article/details/40183565