Java 查询 MMDB 数据库
Lookup.java
:
import java.io.File;
import java.net.InetAddress;
import com.fasterxml.jackson.databind.JsonNode;
import com.maxmind.db.Reader;
public class Lookup {
public static void main(String[] args) throws Exception {
File database = new File("resources/Country.mmdb");
Reader reader = new Reader(database);
// 查询
InetAddress address = InetAddress.getByName("114.114.114.114");
JsonNode response = reader.get(address);
System.out.println(response);
reader.close();
}
}
需要引入 Maxmind DB
和 Jackson Databind
包
pom.xml
:
<project>
...
<dependencies>
...
<dependency>
<groupId>com.maxmind.db</groupId>
<artifactId>maxmind-db</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.16.1</version>
</dependency>
...
</dependencies>
...
</project>
运行结果样例:
{"country":{"is_in_european_union":false,"names":{"de":"China","ru":"Китай","pt-BR":"China","ja":"中国","en":"China","fr":"Chine","zh-CN":"中国","es":"China"},"iso_code":"CN","geoname_id":1814991}}
参考: