优点:免费,可离线运行,准确查询全球城市信息,支持多种客户端,包括Java
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>2.7.0</version>
</dependency>
package cc.iiw.zero.util;
import cn.hutool.core.io.resource.ClassPathResource;
import org.lionsoul.ip2region.xdb.Searcher;
public class IpUtil {
/**
* 国家|区域|省份|城市|ISP
*/
private static String search(String ip) {
try {
String dbPath = new ClassPathResource("ip2region.xdb").getAbsolutePath();
Searcher searcher = Searcher.newWithFileOnly(dbPath);
String region = searcher.search(ip);
searcher.close();
return region;
} catch (Exception e) {
return "";
}
}
public static void main(String[] args) {
System.out.println("国家|区域|省份|城市|ISP");
System.out.println(search("223.104.41.189"));
System.out.println(search("124.236.219.174"));
System.out.println(search("1.2.3.4"));
System.out.println(search("8.8.8.8"));
}
}
国家|区域|省份|城市|ISP
中国|0|北京|北京市|移动
中国|0|河北省|石家庄市|电信
美国|0|华盛顿|0|谷歌
美国|0|0|0|Level3
https://gitee.com/lionsoul/ip2region