1.maven引入:
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-ip2region</artifactId>
<version>2.5.6</version>
</dependency>
2.下载ip2region.db
下载链接:
(128条消息) ip2region.db-Java文档类资源-CSDN文库
3.工具类
import net.dreamlu.mica.ip2region.core.DbConfig;
import net.dreamlu.mica.ip2region.core.DbMakerConfigException;
import net.dreamlu.mica.ip2region.core.DbSearcher;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
String ip = "117.86.21.164";
String db_file = "D:\\work\\ip2region.db";
try {
DbSearcher ipSearch = new DbSearcher(new DbConfig(), db_file);
System.out.println(ipSearch.memorySearch(ip).getRegion());
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (DbMakerConfigException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}