1.x 版本 geoip-api-java https://github.com/maxmind/geoip-api-java
2.x 版本 GeoIP2-java http://maxmind.github.io/GeoIP2-java/
使用Web Service方式
Country Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// This creates a WebServiceClient object that can be reused across requests. // The object is closeable and will keep connections alive for future // requests. Replace "42" with your user ID and "license_key" with your // license key. try (WebServiceClient client = new WebServiceClient.Builder(42, "license_key") .build()) { InetAddress ipAddress = InetAddress.getByName("128.101.101.101"); // Do the lookup CountryResponse response = client.country(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")); // '美国' } |
City Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// This creates a WebServiceClient object that can be reused across requests. // The object is closeable and will keep connections alive for future // requests. Replace "42" with your user ID and "license_key" with your // license key. try (WebServiceClient client = new WebServiceClient.Builder(42, "license_key") .build()) { InetAddress ipAddress = InetAddress.getByName("128.101.101.101"); // Do the lookup CityResponse response = client.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 } |
Insights Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
// This creates a WebServiceClient object that can be reused across requests. // The object is closeable and will keep connections alive for future // requests. Replace "42" with your user ID and "license_key" with your // license key. try (WebServiceClient client = new WebServiceClient.Builder(42, "license_key") .build()) { InetAddress ipAddress = InetAddress.getByName("128.101.101.101"); // Do the lookup InsightsResponse response = client.insights(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")); // '美国' System.out.println(country.getConfidence()); // 99 Subdivision subdivision = response.getMostSpecificSubdivision(); System.out.println(subdivision.getName()); // 'Minnesota' System.out.println(subdivision.getIsoCode()); // 'MN' System.out.println(subdivision.getConfidence()); // 90 City city = response.getCity(); System.out.println(city.getName()); // 'Minneapolis' System.out.println(city.getConfidence()); // 50 Postal postal = response.getPostal(); System.out.println(postal.getCode()); // '55455' System.out.println(postal.getConfidence()); // 40 Location location = response.getLocation(); System.out.println(location.getLatitude()); // 44.9733 System.out.println(location.getLongitude()); // -93.2323 System.out.println(location.getAccuracyRadius()); // 3 System.out.println(location.getTimeZone()); // 'America/Chicago' System.out.println(response.getTraits().getUserType()); // 'college' } |