maxmind提供的免費GeoLite數(shù)據(jù)庫可以使我們簡單方便的對 全球ip 進行過定位蚓耽。下面介紹使用方法 參考自官方, 寫下來留著以后備用:
1 下載mmdb文件數(shù)據(jù)庫和添加依賴
如果官方下載較慢的話也可以使用百度云地址
Maven依賴
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>v2.3.0</version>
</dependency>
2 使用
//GeoIP2-City 數(shù)據(jù)庫文件
File database = new File("/path/to/GeoIP2-City.mmdb");
// 創(chuàng)建 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