<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    聚合 管理  

    Blog Stats

    News

    我使用新博客啦:
    http://www.linjunhai.com/
    大家到我的新博客上看看吧!

    隨筆分類(28)

    文章分類(4)

    隨筆檔案(53)

    文章檔案(4)

    相冊

    相關鏈接


    林俊海的博客

    超級大菜鳥,每天要自強!

    這個東東可以讓你通過IP查詢地址(代碼修改自一個 VBS 的 TQQWry 類)
    程序代碼 程序代碼
    package alvin;
    import java.io.*;

    public class QQWry {
        private String DbPath = "F:\\DB\\QQWry2.Dat";    //純真IP數據庫地址
        private String Country, LocalStr;
        private long IPN;
        private int RecordCount, CountryFlag;
        private long RangE, RangB, OffSet, StartIP, EndIP, FirstStartIP, LastStartIP, EndIPOff;
        private RandomAccessFile fis;
        private byte[] buff;
        
        private long B2L(byte[] b) {
            long ret = 0;
            for (int i=0; i<b.length; i++) {
                long t = 1L;
                for (int j=0; j<i; j++) t = t * 256L;
                ret += ((b[i]<0)?256+b[i]:b[i]) * t;
            }
            return ret;
        }
        
        private long ipToInt(String ip) {
            String[] arr = ip.split("\\.");
            long ret = 0;
            for (int i=0; i<arr.length; i++) {
                long l = 1;
                for (int j=0; j<i; j++) l *= 256;
                try {
                    ret += Long.parseLong(arr[arr.length-i-1]) * l;
                } catch (Exception e) {
                    ret += 0;
                }
            }
            return ret;
        }
        
        public void seek(String ip) throws Exception {
            this.IPN = ipToInt(ip);
            fis = new RandomAccessFile(this.DbPath, "r");
            buff = new byte[4];
            fis.seek(0);
            fis.read(buff);
            FirstStartIP = this.B2L(buff);
            fis.read(buff);
            LastStartIP = this.B2L(buff);
            RecordCount = (int)((LastStartIP - FirstStartIP) / 7);
            if (RecordCount <= 1) {
                LocalStr = Country = "未知";
                throw new Exception();
            }
            
            RangB = 0;
            RangE = RecordCount;
            long RecNo;
            
            do {
                RecNo = (RangB+RangE)/2;
                getStartIP(RecNo);
                if (IPN == StartIP) {
                   RangB = RecNo;
                   break;
                }
                if (IPN > StartIP)
                    RangB = RecNo;
                else
                    RangE = RecNo;
            } while (RangB < RangE-1);
            
            getStartIP(RangB);
            getEndIP();
            getCountry(IPN);
            
            fis.close();
        }

        private String getFlagStr(long OffSet) throws IOException {
            int flag = 0;
            do {
                fis.seek(OffSet);
                buff = new byte[1];
                fis.read(buff);
                flag = (buff[0]<0)?256+buff[0]:buff[0];
                if (flag==1 || flag==2 ) {
                    buff = new byte[3];
                    fis.read(buff);
                    if (flag == 2) {
                        CountryFlag = 2;
                        EndIPOff = OffSet-4;
                    }
                    OffSet = this.B2L(buff);
                } else
                    break;
            } while (true);
            
            if (OffSet < 12) {
                return "";
            } else {
                fis.seek(OffSet);
                return getStr();
            }
        }
        
        private String getStr() throws IOException {
            long l = fis.length();
            ByteArrayOutputStream byteout = new ByteArrayOutputStream();
            byte c  = fis.readByte();
            do {
                byteout.write(c);
                c = fis.readByte();
            } while (c!=0 && fis.getFilePointer() < l);
            return byteout.toString();
        }
        
        private void getCountry(long ip) throws IOException {
            if (CountryFlag == 1 || CountryFlag == 2) {
                Country = getFlagStr(EndIPOff+4);
                if (CountryFlag == 1) {
                    LocalStr = getFlagStr(fis.getFilePointer());
                    if (IPN >= ipToInt("255.255.255.0") && IPN <= ipToInt("255.255.255.255")) {
                        LocalStr = getFlagStr(EndIPOff + 21);
                        Country = getFlagStr(EndIPOff + 12);
                    }
                } else {
                    LocalStr = getFlagStr(EndIPOff+8);
                }
            } else {
                Country = getFlagStr(EndIPOff + 4);
                LocalStr = getFlagStr(fis.getFilePointer());
            }
        }
        
        private long getEndIP() throws IOException {
            fis.seek(EndIPOff);
            buff = new byte[4];
            fis.read(buff);
            EndIP = this.B2L(buff);
            buff = new byte[1];
            fis.read(buff);
            CountryFlag = (buff[0]<0)?256+buff[0]:buff[0];
            return EndIP;
        }
        
        private long getStartIP(long RecNo) throws IOException {
            OffSet = FirstStartIP + RecNo * 7;
            fis.seek(OffSet);
            buff = new byte[4];
            fis.read(buff);
            StartIP = this.B2L(buff);
            buff = new byte[3];
            fis.read(buff);
            EndIPOff = this.B2L(buff);
            return StartIP;
        }
        
        public String getLocal() { return this.LocalStr; }
        public String getCountry() { return this.Country; }
        public void setPath(String path) { this.DbPath = path; }
        
        public static void main(String[] args) throws Exception {
            QQWry w = new QQWry();
            //w.setPath(new File("QQWry2.Dat").getAbsolutePath());
            w.seek("59.39.253.251");
            System.out.println(w.getCountry() + " " + w.getLocal());
        }

    }


    純真IP庫下載地址見: http://www.ialvin.cn/blog/article.asp?id=135
    IP庫大小: 6M多

    如有問題,請到http://www.ialvin.cn/blog/article.asp?id=135提出
    posted on 2007-08-23 14:58 林俊海 閱讀(4470) 評論(0)  編輯  收藏 所屬分類: JAVA天地
    主站蜘蛛池模板: 亚洲av综合日韩| 精品无码专区亚洲| 免费v片在线观看品善网| 99re6热视频精品免费观看| 亚洲国产成人AV网站| 337p日本欧洲亚洲大胆艺术| 亚洲男人的天堂在线va拉文| 免费看h片的网站| a级毛片高清免费视频| 一级做a爱片特黄在线观看免费看| 国产精品亚洲片在线va| 色婷婷亚洲一区二区三区| 亚洲性无码一区二区三区| 亚洲欧洲国产视频| 亚洲三级在线播放| 最新国产成人亚洲精品影院| 亚洲理论片在线中文字幕| 亚洲高清专区日韩精品| 国产亚洲综合网曝门系列| 亚洲人成网址在线观看| 久久精品7亚洲午夜a| 666精品国产精品亚洲| 亚洲人成影院午夜网站| 亚洲AV永久无码精品网站在线观看| 亚洲高清一区二区三区| 亚洲精品国产综合久久久久紧| 黄网站在线播放视频免费观看| 国产成人无码区免费内射一片色欲| 久久福利青草精品资源站免费| 1000部无遮挡拍拍拍免费视频观看 | 成人网站免费观看| 国产片免费在线观看| 国精无码欧精品亚洲一区| 亚洲 欧洲 日韩 综合在线| 一道本不卡免费视频| 亚洲综合图色40p| 免费无码成人AV在线播放不卡| 无码高潮少妇毛多水多水免费| 免费又黄又爽的视频| 亚洲日韩中文字幕| 日本人成在线视频免费播放|