2017年4月12日 星期三

[ Java 套件 ] GeoLite2 - GeoIP2 Java API - Database Example

Prerequisite 
在試用下面範例代碼使用 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 
  1. // A File object pointing to your GeoIP2 or GeoLite2 database  
  2. File database = new File("/path/to/GeoIP2-City.mmdb");  
  3.   
  4. // This creates the DatabaseReader object, which should be reused across  
  5. // lookups.  
  6. DatabaseReader reader = new DatabaseReader.Builder(database).build();  
  7.   
  8. InetAddress ipAddress = InetAddress.getByName("128.101.101.101");  
  9.   
  10. // Replace "city" with the appropriate method for your database, e.g.,  
  11. // "country".  
  12. CityResponse response = reader.city(ipAddress);  
  13.   
  14. Country country = response.getCountry();  
  15. System.out.println(country.getIsoCode());            // 'US'  
  16. System.out.println(country.getName());               // 'United States'  
  17. System.out.println(country.getNames().get("zh-CN")); // '美国'  
  18.   
  19. Subdivision subdivision = response.getMostSpecificSubdivision();  
  20. System.out.println(subdivision.getName());    // 'Minnesota'  
  21. System.out.println(subdivision.getIsoCode()); // 'MN'  
  22.   
  23. City city = response.getCity();  
  24. System.out.println(city.getName()); // 'Minneapolis'  
  25.   
  26. Postal postal = response.getPostal();  
  27. System.out.println(postal.getCode()); // '55455'  
  28.   
  29. Location location = response.getLocation();  
  30. System.out.println(location.getLatitude());  // 44.9733  
  31. System.out.println(location.getLongitude()); // -93.2323  
- Anonymous IP 
  1. // A File object pointing to your GeoIP2 Anonymous IP database  
  2. File database = new File("/path/to/GeoIP2-Anonymous-IP.mmdb");  
  3.   
  4. // This creates the DatabaseReader object, which should be reused across  
  5. // lookups.  
  6. DatabaseReader reader = new DatabaseReader.Builder(database).build();  
  7.   
  8. try {  
  9.     InetAddress ipAddress = InetAddress.getByName("85.25.43.84");  
  10.   
  11.     AnonymousIpResponse response = reader.anonymousIp(ipAddress);  
  12.   
  13.     System.out.println(response.isAnonymous()); // true  
  14.     System.out.println(response.isAnonymousVpn()); // false  
  15.     System.out.println(response.isHostingProvider()); // false  
  16.     System.out.println(response.isPublicProxy()); // false  
  17.     System.out.println(response.isTorExitNode()); //true  
  18. finally {  
  19.     reader.close();  
  20. }  
- Connection-Type 
  1. // A File object pointing to your GeoIP2 Connection-Type database  
  2. File database = new File("/path/to/GeoIP2-Connection-Type.mmdb");  
  3.   
  4. // This creates the DatabaseReader object, which should be reused across  
  5. // lookups.  
  6. DatabaseReader reader = new DatabaseReader.Builder(database).build();  
  7.   
  8. InetAddress ipAddress = InetAddress.getByName("128.101.101.101");  
  9.   
  10. ConnectionTypeResponse response = reader.connectionType(ipAddress);  
  11.   
  12. // getConnectionType() returns a ConnectionType enum  
  13. ConnectionType type = response.getConnectionType();  
  14.   
  15. System.out.println(type); // 'Corporate'  
- Domain 
  1. // A File object pointing to your GeoIP2 Domain database  
  2. File database = new File("/path/to/GeoIP2-Domain.mmdb");  
  3.   
  4. // This creates the DatabaseReader object, which should be reused across  
  5. // lookups.  
  6. DatabaseReader reader = new DatabaseReader.Builder(database).build();  
  7.   
  8. InetAddress ipAddress = InetAddress.getByName("128.101.101.101");  
  9.   
  10. DomainResponse response = reader.domain(ipAddress);  
  11.   
  12. System.out.println(response.getDomain()); // 'Corporate'  
- Enterprise 
  1. // A File object pointing to your GeoIP2 Enterprise database  
  2. File database = new File("/path/to/GeoIP2-Enterprise.mmdb");  
  3.   
  4. // This creates the DatabaseReader object, which should be reused across  
  5. // lookups.  
  6. try (DatabaseReader reader = new DatabaseReader.Builder(database).build()) {  
  7.     InetAddress ipAddress = InetAddress.getByName("128.101.101.101");  
  8.   
  9.     //  Use the enterprise(ip) method to do a lookup in the Enterprise database  
  10.     EnterpriseResponse response = reader.enterprise(ipAddress);  
  11.   
  12.     Country country = response.getCountry();  
  13.     System.out.println(country.getIsoCode());            // 'US'  
  14.     System.out.println(country.getName());               // 'United States'  
  15.     System.out.println(country.getNames().get("zh-CN")); // '美国'  
  16.     System.out.println(country.getConfidence());         // 99  
  17.   
  18.     Subdivision subdivision = response.getMostSpecificSubdivision();  
  19.     System.out.println(subdivision.getName());           // 'Minnesota'  
  20.     System.out.println(subdivision.getIsoCode());        // 'MN'  
  21.     System.out.println(subdivision.getConfidence());     // 77  
  22.   
  23.     City city = response.getCity();  
  24.     System.out.println(city.getName());       // 'Minneapolis'  
  25.     System.out.println(city.getConfidence()); // 11  
  26.   
  27.     Postal postal = response.getPostal();  
  28.     System.out.println(postal.getCode()); // '55455'  
  29.     System.out.println(postal.getConfidence()); // 5  
  30.   
  31.     Location location = response.getLocation();  
  32.     System.out.println(location.getLatitude());  // 44.9733  
  33.     System.out.println(location.getLongitude()); // -93.2323  
  34.     System.out.println(location.getAccuracyRadius()); // 50  
  35. }  
- ISP 
  1. // A File object pointing to your GeoIP2 ISP database  
  2. File database = new File("/path/to/GeoIP2-ISP.mmdb");  
  3.   
  4. // This creates the DatabaseReader object, which should be reused across  
  5. // lookups.  
  6. DatabaseReader reader = new DatabaseReader.Builder(database).build();  
  7.   
  8. InetAddress ipAddress = InetAddress.getByName("128.101.101.101");  
  9.   
  10. IspResponse response = reader.isp(ipAddress);  
  11.   
  12. System.out.println(response.getAutonomousSystemNumber());       // 217  
  13. System.out.println(response.getAutonomousSystemOrganization()); // 'University of Minnesota'  
  14. System.out.println(response.getIsp());                          // 'University of Minnesota'  
  15. 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

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...