czdb技术文档:https://github.com/tagphi/czdb-search-java
具体操作:一、下载安装eclipse
Eclipse下载安装教程(包含JDK安装)【保姆级教学】【2024.4已更新】-CSDN博客
二、eclipse下载安装Maven
Maven工程的安装配置及搭建(集成eclipse完成案例,保姆级教学)_eclipse 配置maven-CSDN博客
三、eclipse新建Maven项目
Eclipse搭建Maven项目_eclipse创建maven项目-CSDN博客
注:选择quickstart,不选择web。
四、配置Maven项目pom.xml,添加如下依赖
<dependency>
<groupId>net.cz88</groupId>
<artifactId>czdb-search</artifactId>
<version>1.0.2.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.10</version>
</dependency>
五、编写Maven项目main下面的app.java
package com.csdn.jh.jhip;
import net.cz88.czdb.DbSearcher;
import net.cz88.czdb.QueryType;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class App {
public static void main(String[] args) {
String dbFilePath = "D:/Users/pc/Desktop/temp/czdb/cz88_public_v4.czdb";
String key = "从网站获取的自己的秘钥";
String ipInputFile = "D:/Users/pc/Desktop/temp/czdb/ip6.txt";
String outputFile = "D:/Users/pc/Desktop/temp/czdb/output.txt";
DbSearcher searcher = null;
BufferedReader ipReader = null;
BufferedWriter resultWriter = null;
try {
searcher = new DbSearcher(dbFilePath, QueryType.MEMORY, key);
ipReader = new BufferedReader(new FileReader(ipInputFile));
resultWriter = new BufferedWriter(new FileWriter(outputFile));
String ip;
while ((ip = ipReader.readLine()) != null) {
String region = searcher.search(ip);
resultWriter.write(ip + "\t" + (region != null ? region : "N/A") + "\n");
}
System.out.println("查询完成,结果已保存到 " + outputFile);
} catch (Exception e) {
e.printStackTrace(); // 或者使用日志记录异常
// 这里不抛出新的RuntimeException
} finally {
try {
if (resultWriter != null) resultWriter.close();
if (ipReader != null) ipReader.close();
if (searcher != null && searcher instanceof AutoCloseable) {
((AutoCloseable) searcher).close();
}
}
catch (Exception e) {
e.printStackTrace(); // 或者使用日志记录异常
}
}
}
}
六、把需要查询的IP复制到D:/Users/pc/Desktop/temp/czdb/ip6.txt
七、运行app.java 。run as java application
成功
附上eclipse截图
附上运行成果图
如果需要查询IPv6,将数据库文件换为cz88_public_v6.czdb,就行了,其他不变。