溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用ip-attribution.dat文件實(shí)現(xiàn)IP歸屬地查詢

發(fā)布時(shí)間:2021-06-24 14:02:23 來源:億速云 閱讀:159 作者:chen 欄目:大數(shù)據(jù)

這篇文章主要介紹“使用ip-attribution.dat文件實(shí)現(xiàn)IP歸屬地查詢”,在日常操作中,相信很多人在使用ip-attribution.dat文件實(shí)現(xiàn)IP歸屬地查詢問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”使用ip-attribution.dat文件實(shí)現(xiàn)IP歸屬地查詢”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

使用ip-attribution.dat文件實(shí)現(xiàn)IP歸屬地查詢。下載地址
網(wǎng)盤下載 提取碼:2chr 

package com.sky.common.utils;

import cn.hutool.core.io.resource.ClassPathResource;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * ip歸屬地解析
 *
 * @author Moses wsj1198878990@126.com
 * @since 1.0.0 2019-10-22
 */
public final class IPDataHandler {
    private static DataInputStream inputStream = null;
    private static long fileLength = -1;
    private static int dataLength = -1;
    private static Map<String, String> cacheMap = null;
    private static byte[] allData = null;

    static {
//		File file = new File(IP_DATA_PATH);
        try {
            //把文件放到src/main/resources下面
            File file = new ClassPathResource("ip-attribution.dat").getFile();
            inputStream = new DataInputStream(new FileInputStream(file));
            fileLength = file.length();
            cacheMap = new HashMap<String, String>();
            if (fileLength > Integer.MAX_VALUE) {
                throw new Exception("the filelength over 2GB");
            }

            dataLength = (int) fileLength;
            allData = new byte[dataLength];
            inputStream.read(allData, 0, dataLength);
            dataLength = (int) getbytesTolong(allData, 0, 4,
                    ByteOrder.BIG_ENDIAN);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static long getbytesTolong(byte[] bytes, int offerSet, int size,
                                       ByteOrder byteOrder) {
        if ((offerSet + size) > bytes.length || size <= 0) {
            return -1;
        }
        byte[] b = new byte[size];
        for (int i = 0; i < b.length; i++) {
            b[i] = bytes[offerSet + i];
        }
        ByteBuffer byteBuffer = ByteBuffer.wrap(b);
        byteBuffer.order(byteOrder);
        long temp = -1;
        if (byteBuffer.hasRemaining()) {
            temp = byteBuffer.getInt();
        }
        return temp;
    }

    private static long ip2long(String ip) throws UnknownHostException {
        InetAddress address = InetAddress.getByName(ip);
        byte[] bytes = address.getAddress();
        long reslut = getbytesTolong(bytes, 0, 4, ByteOrder.BIG_ENDIAN);
        return reslut;
    }

    private static int getIntByBytes(byte[] b, int offSet) {
        if (b == null || (b.length < (offSet + 3))) {
            return -1;
        }
        byte[] bytes = Arrays.copyOfRange(allData, offSet, offSet + 3);
        byte[] bs = new byte[4];
        bs[3] = 0;
        for (int i = 0; i < 3; i++) {
            bs[i] = bytes[i];
        }
        return (int) getbytesTolong(bs, 0, 4, ByteOrder.LITTLE_ENDIAN);
    }

    public static String findGeography(String address) {
        if (StringUtils.isBlank(address)) {
            return "illegal address";
        }
        if (dataLength < 4 || allData == null) {
            return "illegal ip data";
        }
        String ip = "127.0.0.1";
        try {
            ip = Inet4Address.getByName(address).getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String[] ipArray = StringUtils.split(ip, ".");
        int ipHeadValue = Integer.parseInt(ipArray[0]);
        if (ipArray.length != 4 || ipHeadValue < 0 || ipHeadValue > 255) {
            return "illegal ip";
        }
        if (cacheMap.containsKey(ip)) {
            return cacheMap.get(ip);
        }
        long numIp = 1;
        try {
            numIp = ip2long(address);
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        }
        int tempOffSet = ipHeadValue * 4 + 4;
        long start = getbytesTolong(allData, tempOffSet, 4,
                ByteOrder.LITTLE_ENDIAN);
        int max_len = dataLength - 1028;
        long resultOffSet = 0;
        int resultSize = 0;
        for (start = start * 8 + 1024; start < max_len; start += 8) {
            if (getbytesTolong(allData, (int) start + 4, 4,
                    ByteOrder.BIG_ENDIAN) >= numIp) {
                resultOffSet = getIntByBytes(allData, (int) (start + 4 + 4));
                resultSize = (char) allData[(int) start + 7 + 4];
                break;
            }
        }
        if (resultOffSet <= 0) {
            return "resultOffSet too small";
        }
        byte[] add = Arrays.copyOfRange(allData, (int) (dataLength
                        + resultOffSet - 1024),
                (int) (dataLength + resultOffSet - 1024 + resultSize));
        try {
            if (add == null) {
                cacheMap.put(ip, new String("no data found!!"));
            } else {
                cacheMap.put(ip, new String(add, "UTF-8"));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return cacheMap.get(ip);
    }

    public static void main(String[] args) {
        String s = findGeography("61.164.169.58");
        System.out.println(s.trim());
    }
}

到此,關(guān)于“使用ip-attribution.dat文件實(shí)現(xiàn)IP歸屬地查詢”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

dat
AI