针对IP地址的解析为城市地址:
需要用到github上的第三方开源库==>ipdatabase
https://github.com/wzhe06/ipdatabase
功能:根据IP来查询城市接口
使用github上已有的开源项目
1)git clone https://github.com/wzhe06/ipdatabase.git
2)编译下载的项目:mvn clean package -DskipTests(直接在xshell本地进行编译,在ipdatabase根目录执行编译命令)
3)安装jar包到自己的maven仓库
mvn install:install-file -Dfile=D:\maven\repository\ipdatabase-master\target\ipdatabase-1.0-SNAPSHOT.jar -DgroupId=com.ggstar -DartifactId=ipdatabase -Dversion=1.0 -Dpackaging=jar
4)在pom.xm中加入
<!--ip城市--> <dependency> <groupId>com.ggstar</groupId> <artifactId>ipdatabase</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency>
5)把编译好的ipdatabase中的ipRegion.xlsx和ipDatabase.csv拷贝到idea里的Resource目录里
6)ip解析工具类
**
* ip解析工具类 把ip转化为城市
*/
object IpUtils {
def getCity(ip: String) = {
IpHelper.findRegionByIp(ip)
}
/**
* 测试
* @param args
*/
def main(args: Array[String]): Unit = {
println(getCity("10.66.8.230"))
}
}
最后成功
--->val city = IpUtils.getCity(ip)