在試用下面範例代碼使用 IP 找對應 Geo location 前, 請先下載對應 DB 與 Java GeoLite2 library:
* GeoLite2 Free Downloadable Databases
* GeoIP2-java Github (geoip2-2.8.1-with-dependencies.zip)
Database Example (Source)
- City
- // A File object pointing to your GeoIP2 or GeoLite2 database
- File database = new File("/path/to/GeoIP2-City.mmdb");
- // This creates the DatabaseReader object, which should be reused across
- // lookups.
- DatabaseReader reader = new DatabaseReader.Builder(database).build();
- InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
- // Replace "city" with the appropriate method for your database, e.g.,
- // "country".
- 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
- // A File object pointing to your GeoIP2 Anonymous IP database
- File database = new File("/path/to/GeoIP2-Anonymous-IP.mmdb");
- // This creates the DatabaseReader object, which should be reused across
- // lookups.
- DatabaseReader reader = new DatabaseReader.Builder(database).build();
- try {
- InetAddress ipAddress = InetAddress.getByName("85.25.43.84");
- AnonymousIpResponse response = reader.anonymousIp(ipAddress);
- System.out.println(response.isAnonymous()); // true
- System.out.println(response.isAnonymousVpn()); // false
- System.out.println(response.isHostingProvider()); // false
- System.out.println(response.isPublicProxy()); // false
- System.out.println(response.isTorExitNode()); //true
- } finally {
- reader.close();
- }
- // A File object pointing to your GeoIP2 Connection-Type database
- File database = new File("/path/to/GeoIP2-Connection-Type.mmdb");
- // This creates the DatabaseReader object, which should be reused across
- // lookups.
- DatabaseReader reader = new DatabaseReader.Builder(database).build();
- InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
- ConnectionTypeResponse response = reader.connectionType(ipAddress);
- // getConnectionType() returns a ConnectionType enum
- ConnectionType type = response.getConnectionType();
- System.out.println(type); // 'Corporate'
- // A File object pointing to your GeoIP2 Domain database
- File database = new File("/path/to/GeoIP2-Domain.mmdb");
- // This creates the DatabaseReader object, which should be reused across
- // lookups.
- DatabaseReader reader = new DatabaseReader.Builder(database).build();
- InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
- DomainResponse response = reader.domain(ipAddress);
- System.out.println(response.getDomain()); // 'Corporate'
- // A File object pointing to your GeoIP2 Enterprise database
- File database = new File("/path/to/GeoIP2-Enterprise.mmdb");
- // This creates the DatabaseReader object, which should be reused across
- // lookups.
- try (DatabaseReader reader = new DatabaseReader.Builder(database).build()) {
- InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
- // Use the enterprise(ip) method to do a lookup in the Enterprise database
- EnterpriseResponse response = reader.enterprise(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()); // 77
- City city = response.getCity();
- System.out.println(city.getName()); // 'Minneapolis'
- System.out.println(city.getConfidence()); // 11
- Postal postal = response.getPostal();
- System.out.println(postal.getCode()); // '55455'
- System.out.println(postal.getConfidence()); // 5
- Location location = response.getLocation();
- System.out.println(location.getLatitude()); // 44.9733
- System.out.println(location.getLongitude()); // -93.2323
- System.out.println(location.getAccuracyRadius()); // 50
- }
- // A File object pointing to your GeoIP2 ISP database
- File database = new File("/path/to/GeoIP2-ISP.mmdb");
- // This creates the DatabaseReader object, which should be reused across
- // lookups.
- DatabaseReader reader = new DatabaseReader.Builder(database).build();
- InetAddress ipAddress = InetAddress.getByName("128.101.101.101");
- IspResponse response = reader.isp(ipAddress);
- System.out.println(response.getAutonomousSystemNumber()); // 217
- System.out.println(response.getAutonomousSystemOrganization()); // 'University of Minnesota'
- System.out.println(response.getIsp()); // 'University of Minnesota'
- System.out.println(response.getOrganization()); // 'University of Minnesota'
Supplement
* MaxMind GeoIP2 API 2.8.1 API
* Free Geolocation Lookup - Where is This Website or IP Address?
* Java – find location using Ip Address
沒有留言:
張貼留言