IP地址到国家与数据库

new*_*bie 1 java geolocation ip-geolocation

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

Omr*_*dan 5

我写了一个名为ip2c的小lib 来做到这一点.它使用来自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());      
}
Run Code Online (Sandbox Code Playgroud)