千家信息网

使用ip-attribution.dat文件实现IP归属地查询

发表于:2025-02-06 作者:千家信息网编辑
千家信息网最后更新 2025年02月06日,这篇文章主要介绍"使用ip-attribution.dat文件实现IP归属地查询",在日常操作中,相信很多人在使用ip-attribution.dat文件实现IP归属地查询问题上存在疑惑,小编查阅了各
千家信息网最后更新 2025年02月06日使用ip-attribution.dat文件实现IP归属地查询

这篇文章主要介绍"使用ip-attribution.dat文件实现IP归属地查询",在日常操作中,相信很多人在使用ip-attribution.dat文件实现IP归属地查询问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"使用ip-attribution.dat文件实现IP归属地查询"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

使用ip-attribution.dat文件实现IP归属地查询。下载地址
网盘下载 提取码: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 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();            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());    }}

到此,关于"使用ip-attribution.dat文件实现IP归属地查询"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

文件 归属 查询 学习 更多 帮助 实用 接下来 地址 文章 方法 理论 知识 篇文章 网站 资料 跟着 问题 UTF-8 好用 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 网络安全法促进中国特色国家安全 销售网络安全年终总结 网络安全为人民的意思 扫描网络安全下载 云南高考历年录取分数线数据库 国际主流的数据库有哪些 最新网络安全论文 云服务器是不是无限流量 学软件开发的考什么证比较好 网络安全与校园贷知识竞赛 梦幻西游手游梦幻的夏天服务器 etc用哪个软件开发票 军贸软件开发文档 标准 上海自动化网络技术值多少钱 如何设计数据库j 代还APP软件开发公司 榆树网络技术服务诚信合作 女士学动漫设计还是软件开发 魏秋明网络技术服务 2008r2数据库安装不成功 郑州善悦网络技术 海康服务器和存储的连接方法 国考网络技术三级 jsp表格从数据库读取数据 送网络技术人员锦旗 刀片服务器机 上海铁路局网络安全工程师招聘 hbase清空数据库 山东有网络安全专业的大学吗 LTE无线通信网络安全防御措施
0