java - 带数据库的国家/地区的 IP 地址

标签 java geolocation ip-geolocation

<分区>

我已经下载了 ip-to-country.csv,它具有映射到国家/地区的 ip 范围。我应该如何将这些数据存储到数据库中,以及如何查询 IP 地址在哪个范围内以了解 IP 地址来自哪里?

最佳答案

我写了一个名为 ip2c 的小库做到这一点。它使用来自 webhosting.info 的数据库但也支持 Software77 .

它将 CSV 信息转换为紧凑的二进制格式,可以直接在文件、内存或内存映射文件中进行搜索。

Java API 用法与此类似:

String ip = 85.64.225.159;
int caching1 = IP2Country.NO_CACHE;  // Straight on file, Fastest startup, slowest queries
int caching2 = IP2Country.MEMORY_MAPPED; // Memory mapped file, fast startup, fast queries.
int caching3 = IP2Country.MEMORY_CACHE; // load file into memory, slowerst startup, fastest queries
IP2Country ip2c = new IP2Country(caching1);
Country c = ip2c.getCountry(ip);
if (c == null)
{
        System.out.println("UNKNOWN");                          
}
else
{
        // will output IL ISR ISRAEL
        System.out.println(c.get2cStr() + " " + c.get3cStr() + " " + c.getName());      
}

关于java - 带数据库的国家/地区的 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2775655/

相关文章:

java - 控制反转与好莱坞原则的混淆

python - 计算 Pandas 地理密度的有效方法?

android-emulator - Android Geolocation 无法在模拟器中获取您的位置错误

mysql更新地理点

java - 3D 三边测量 Java

geolocation - IP 地理定位如何工作?

java - 将数字字符串转换为整数列表

java - Java 中的泛型类型

java - CompletableFuture.thenAccept 中使用的垃圾回收对象的可用性

html - Web浏览器中的位置检测