您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页使用 GeoLite 实现IP精准定位(Java实现)

使用 GeoLite 实现IP精准定位(Java实现)

来源:二三四教育网

1 下载mmdb文件数据库和添加依赖

Maven依赖

<dependency> 
    <groupId>com.maxmind.geoip2</groupId> 
    <artifactId>geoip2</artifactId>
    <version>v2.3.0</version>
</dependency>

2 使用

//GeoIP2-City 数据库文件
File database = new File("/path/to/GeoIP2-City.mmdb");

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

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

CityResponse response = reader.city(ipAddress);

Country country = response.getCountry();
System.out.println(country.getIsoCode());            // 'US'
System.out.println(country.getName());               // 'United States'
System.out.println(country.getNames().get("zh-CN")); // '美国'

Subdivision subdivision = response.getMostSpecificSubdivision();
System.out.println(subdivision.getName());    // 'Minnesota'
System.out.println(subdivision.getIsoCode()); // 'MN'

City city = response.getCity();
System.out.println(city.getName()); // 'Minneapolis'

Postal postal = response.getPostal();
System.out.println(postal.getCode()); // '55455'

Location location = response.getLocation();
System.out.println(location.getLatitude());  // 44.9733
System.out.println(location.getLongitude()); // -93.2323

参考

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务