Java IP 地址服务 GeoIP

本文内容

  • 介绍
  • 动手

什么是GeoIP

地理知识产权数据库与服务:业界领先的知识产权情报

Geoip 是 MaxMind 的IP智能产品GeoIP由多个相关产品组成。GeoIP允许您发现关于特定IP地址的信息。提供web服务、基于订阅的可下载数据库和免费的可下载数据库,MaxMind GeoIP2产品为内容个性化、欺诈检测、广告定位、流量分析、遵从性、地理定位、地理防护和数字版权管理等广泛应用程序识别互联网用户的位置和其他特征。
[MaxMind官网]https://www.maxmind.com/en/home

GeoIP 有收费版本 GeoIP2 和 GeoLite2 ,GeoLite2 为免费版本。基于中国互联网思维我直接就看了 GeoLite2免费下载数据库

GeoLite2数据库

GeoLite2数据库是免费的IP地理定位数据库,与MaxMind的GeoIP2数据库类似,但不那么精确。GeoLite2国家和城市数据库在每个月的第一个星期二更新。GeoLite2 ASN数据库每周二更新。

ip地理位置的使用

IP地理定位本身就是不精确的。地点通常靠近人口的中心。GeoIP数据库提供的任何位置都不应用于标识特定的地址或家庭。使用精度半径作为我们返回的IP地址的经纬度坐标的地理定位精度的指示。IP地址的实际位置可能在此半径和经纬度坐标定义的区域内。

官方支持

MaxMind不为免费的GeoLite2数据库提供官方支持。如果您对GeoLite2数据库或GeoIP2 api有疑问,请参阅stackoverflow的GeoIP问答。GeoLite2数据库是在知识共享署名- sharealike 4.0国际许可下发布的。通过在所有提及或使用此数据库的特性的广告和文档中包含以下内容,可以满足属性要求

MaxMind还为GeoLite2数据库提供再分发许可证,允许您使用商业产品打包数据库。 商业再分发许可证旨在为企业提供在其产品中包含IP地址地理定位技术的能力。GeoLite2商业重新发布许可允许您免费发布GeoLite2数据库(包括更新)一年。请注意,GeoLite2商业再发行许可证中包含的GeoLite2数据库并不像我们的GeoIP2数据库那样准确。有关更多信息,请访问GeoLite2商业再分发页面。[visit the GeoLite2 Commercial Redistribution page.] https://www.maxmind.com/en/geolite2-commercial-redistribution

动手开始跟我一起做

导入maven

 <dependency>
        <groupId>com.maxmind.db</groupId>
        <artifactId>maxmind-db</artifactId>
        <version>1.2.2</version>
    </dependency>

下载数据包

https://geolite.maxmind.com/download/geoip/database/GeoLite2-City-CSV.zip csv

https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz 下载地址 MaxMind DB binary, gzipped

编码

public class GeoipTest {
    public static void main(String[] args) throws Exception {
        //GeoIP2-City 数据库文件D
        File database = new File("/Users/yangyibo/test/GeoLite2-City/GeoLite2-City.mmdb");

        // 创建 DatabaseReader对象
        DatabaseReader reader = new DatabaseReader.Builder(database).build();

        InetAddress ipAddress = InetAddress.getByName("127.0.0.1");

        // 获取查询结果
        CityResponse response = reader.city(ipAddress);

        // 获取国家信息
        Country country = response.getCountry();
        System.out.println("国家code:"+country.getIsoCode());
        System.out.println("国家:"+country.getNames().get("zh-CN"));

        // 获取省份
        Subdivision subdivision = response.getMostSpecificSubdivision();
        System.out.println("省份code:"+subdivision.getIsoCode());
        System.out.println("省份:"+subdivision.getNames().get("zh-CN"));

        //城市
        City city = response.getCity();
        System.out.println("城市code:"+city.getGeoNameId());
        System.out.println("城市:"+city.getName());

        // 获取城市
        Location location = response.getLocation();
        System.out.println("经度:"+location.getLatitude());
        System.out.println("维度:"+location.getLongitude());

    }

}

API 内部实现 将二进制数据库文件加载到Java内存,在 Java内存中构建一个 二叉查找树。算法“https://maxmind.github.io/MaxMind-DB/”

官方 git https://github.com/maxmind/GeoIP2-java

发布了343 篇原创文章 · 获赞 649 · 访问量 231万+

猜你喜欢

转载自blog.csdn.net/u012373815/article/details/98219485
今日推荐