1.下载GeoLite2 City库
下载地址:
http://dev.maxmind.com/geoip/geoip2/geolite2/
https://download.csdn.net/download/qq_44065303/20680263
2.导入相关坐标坐标
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
3.代码实现
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.*;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
/**
* @author yijiu
* @date 2021/8/2 15:04
*/
public class IpAddress {
public static void main(String[] args) throws Exception{
String ip = "180.117.160.226";
IpAddress ipAddressController = new IpAddress();
// 获取国家信息
String country = ipAddressController.getCountry(ip);
System.out.println("country:"+country);
// 获取省份
String province = ipAddressController.getProvince(ip);
System.out.println("province:"+province);
// 获取城市
String city = ipAddressController.getCity(ip);
System.out.println("city:"+city);
}
private String getCity(String ip) throws IOException, GeoIp2Exception {
// 创建 GeoLite2 数据库
File database = new File("GeoLite2-City.mmdb");//这里是下载的GeoLite2 City库的路径
// 读取数据库内容
DatabaseReader reader = new DatabaseReader.Builder(database).build();
InetAddress ipAddress = InetAddress.getByName(ip);
// 获取查询结果
CityResponse response = reader.city(ipAddress);
//获取City
City city = response.getCity();
// System.out.println(city.getName()); // 'Nanning'
// Postal postal = response.getPostal();
// System.out.println(postal.getCode()); // 'null'
// System.out.println(city.getNames().get("zh-CN")); // '南宁'
// Location location = response.getLocation();
// System.out.println(location.getLatitude()); // 22.8167
// System.out.println(location.getLongitude()); // 108.3167
return city.getNames().get("zh-CN");
}
private String getProvince(String ip) throws IOException, GeoIp2Exception {
// 创建 GeoLite2 数据库
File database = new File("GeoLite2-City.mmdb");
// 读取数据库内容
DatabaseReader reader = new DatabaseReader.Builder(database).build();
InetAddress ipAddress = InetAddress.getByName(ip);
// 获取查询结果
CityResponse response = reader.city(ipAddress);
//获取Province
Subdivision subdivision = response.getMostSpecificSubdivision();
// System.out.println(subdivision.getName()); // 'Guangxi Zhuangzu Zizhiqu'
// System.out.println(subdivision.getIsoCode()); // '45'
// System.out.println(subdivision.getNames().get("zh-CN")); // '广西壮族自治区'
return subdivision.getNames().get("zh-CN");
}
private String getCountry(String ip) throws IOException, GeoIp2Exception {
// 创建 GeoLite2 数据库
File database = new File("GeoLite2-City.mmdb");
// 读取数据库内容
DatabaseReader reader = new DatabaseReader.Builder(database).build();
InetAddress ipAddress = InetAddress.getByName(ip);
// 获取查询结果
CityResponse response = reader.city(ipAddress);
//获取country
Country country = response.getCountry();
// System.out.println(country.getIsoCode()); // 'CN'
// System.out.println(country.getName()); // 'China'
// System.out.println(country.getNames().get("zh-CN")); // '中国'
return country.getNames().get("zh-CN");
}
}
输入结果
country:中国
province:江苏省
city:苏州市