最近各大平台都在显示用户的地址,本章介绍如何将ip转地址。
代码中使用到的库文件基于ipdb,官网地址:https://www.ipip.net/
本章代码已分享至Gitee:https://gitee.com/lengcz/springboot-ipdb.git
- 引入依赖
net.ipip
ipdb
1.1.3
- 编写代码
package com.lengcz.springbootipdb.util;
import net.ipip.ipdb.City;
import java.io.IOException;
public class IpdbUtil {
private static City city_DB;
static {
try {
city_DB = new City(new IpdbUtil().getClass().getResource("/").getPath() + "ipipfree.ipdb");
} catch (IOException e) {
e.printStackTrace();
}
}
public static String[] find(String ip, String language) {
try {
return city_DB.find(ip, language);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
- 将ipipfree.db存入资源文件下(源码中已包含)
- 测试
System.out.println(Arrays.toString(IpdbUtil.find("123.125.71.38", "CN")));
5. 启动项目,可以查到ip所对应的地址。



