根据ip地址获得城市地址
转自:https://blog.csdn.net/m0_66167035/article/details/124628268
Ip2region是什么?
    ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nodejs,golang,c#等查询绑定和Binary,B树,内存三种查询算法。 
Ip2region特性
1.99.9%准确率
2.数据聚合了一些知名ip到地名查询提供商的数据,这些是他们官方的的准确率,经测试着实比经典的纯真IP定位准确一些。
3.ip2region的数据聚合自以下服务商的开放API或者数据(升级程序每秒请求次数2到4次):
4.ps:如果上述开放API或者数据都不给开放数据时ip2region将停止数据的更新服务。
标准化的数据格式
每条ip数据段都固定了格式:
城市Id|国家|区域|省份|城市|ISP
查询速度快
全部的查询客户端单次查询都在0.x毫秒级别,内置了三种查询算法:
1.memory算法:整个数据库全部载入内存,单次查询都在0.1x毫秒内,C语言的客户端单次查询在0.00x毫秒级别。
2.binary算法:基于二分查找,基于ip2region.db文件,不需要载入内存,单次查询在0.x毫秒级别。
3.b-tree算法:基于btree算法,基于ip2region.db文件,不需要载入内存,单词查询在0.x毫秒级别,比binary算法更快。
ps:任何客户端b-tree都比binary算法快,当然memory算法固然是最快的!
 
maven集成:
1.引入依赖:
<dependencies>
        <dependency>
            <groupId>org.lionsoul</groupId>
            <artifactId>ip2region</artifactId>
            <version>1.7.2</version>
        </dependency>
    </dependencies>
2.下载ip2region.db 
3.将ip2region.db放入项目中的src/main/resource文件夹下
4.精彩的内容到了,就开始了代码
package utils;
 
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;
 
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
 
public class IPUtil {
 
    /**
     * 根据IP地址获取城市
     * @param ip
     * 如果放在服务器读取不了,两种方式
     * 1.可以在服务器上创建文件,ip2region.db这个放在文件里面,然后开始读取
     * 2.可以整个配置文件,在配置配置目录,然后读取配置文件
     * @return
     */
    public static String getCityInfo(String ip) throws IOException {
        URL url = IPUtil.class.getClassLoader().getResource("ip2region.db");
        File file;
        if (url != null) {
            file = new File(url.getFile());
        } else {
            return null;
        }
        if (!file.exists()) {
            System.out.println("Error: Invalid ip2region.db file, filePath:" + file.getPath());
            return null;
        }
        //查询算法
        int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
        //DbSearcher.BINARY_ALGORITHM //Binary
        //DbSearcher.MEMORY_ALGORITYM //Memory
        try {
            DbConfig config = new DbConfig();
            DbSearcher searcher = new DbSearcher(config, file.getPath());
            Method method;
            switch ( algorithm )
            {
                case DbSearcher.BTREE_ALGORITHM:
                    method = searcher.getClass().getMethod("btreeSearch", String.class);
                    break;
                case DbSearcher.BINARY_ALGORITHM:
                    method = searcher.getClass().getMethod("binarySearch", String.class);
                    break;
                case DbSearcher.MEMORY_ALGORITYM:
                    method = searcher.getClass().getMethod("memorySearch", String.class);
                    break;
                default:
                    return null;
            }
            DataBlock dataBlock;
            if (!Util.isIpAddress(ip)) {
                System.out.println("Error: Invalid ip address");
                return null;
            }
            dataBlock  = (DataBlock) method.invoke(searcher, ip);
            return dataBlock.getRegion();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
}
 
package ceshi;
 
import utils.IPUtil;
 
import java.io.IOException;
import java.net.InetAddress;
 
 
public class CeShiIp {
 
    public static void main(String[] args) throws IOException {
        //获取IP地址
        String hostAddress = InetAddress.getLocalHost().getHostAddress();
        //工具类
        String detail = IPUtil.getCityInfo(hostAddress);
        System.out.println("IP地址:"+hostAddress+"地区:"+detail);
    }
}
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号