基于maxmind ip地址查询服务

资源下载地址
http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz
http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz

http://dev.maxmind.com/zh-hans/geoip/geoip2/geolite2-开源数据库/

maxmind demo地址
http://maxmind.github.io/GeoIP2-java/

Java  jar 包导入
<dependency>
        <groupId>com.maxmind.geoip2</groupId>
        <artifactId>geoip2</artifactId>
        <version>2.1.0</version>
    </dependency>

======================================================================
Java demo
try{
   // A File object pointing to your GeoIP2 or GeoLite2 database
   //数据文件路径
   File database = new File("${base_path}\\GeoLite2-City.mmdb");
   
   // This creates the DatabaseReader object, which should be reused across
   // lookups.
   DatabaseReader reader = new DatabaseReader.Builder(database).build();   
   InetAddress ipAddress = InetAddress.getByName("123.57.12.158");   
   // 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.getNames().get("zh-CN"));    // 'Minnesota' //省份中文名  英文名:subdivision.getName()
   System.out.println(subdivision.getIsoCode()); // 'MN'
   
   City city = response.getCity();
   System.out.println(city.getNames().get("zh-CN")); // '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 //经度
   
  }catch(Exception e){
   e.printStackTrace();
  }
======================================================================

你可能感兴趣的:(IP地址)