Ip address to country with database(带有数据库的国家/地区的 IP 地址)
问题描述
我已下载 ip-to-country.csv,其中包含映射到国家/地区的 IP 范围.我应该如何将这些数据存储到数据库中,如何查询 IP 地址在什么范围内才能知道 IP 地址来自哪里?
I have downloaded ip-to-country.csv that has ip ranges that are mapped to countries. How should I store this data to database and how can I query in what range Ip address is to know where Ip address is coming from?
推荐答案
我写了一个小库,叫做 ip2c 做到这一点.它使用来自 webhosting.info 的数据库,但也支持来自 Software77.
I wrote a small lib called ip2c to do just that. it uses the database from webhosting.info but also supports that from Software77.
它将 CSV 信息转换为紧凑的二进制格式,并且可以直接在文件、内存或内存映射文件中进行搜索.
It converts the CSV info a compact binary format and can do the search straight on the file, in memory or in a memory mapped file.
Java API 用法类似:
The Java API usage is similar to this:
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());
}
这篇关于带有数据库的国家/地区的 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有数据库的国家/地区的 IP 地址
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01