1. Download the offline analysis library
Link: https://pan.baidu.com/s/1hbkROxucS4V7IHY_h82wEQ
Extraction code: agct
Official website address
https://github.com/lionsoul2014/ip2region/tree/master/data
2. Import maven dependencies
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>1.7.2</version>
</dependency>
3. Custom tools
package test
import org.lionsoul.ip2region.{
DbConfig, DbSearcher}
object iputil {
def main(args: Array[String]): Unit = {
val dbFile = "E:\\test\\final_proj\\src\\main\\resources\\ip2region.db"
val ip = "110.85.18.234"
val search = new DbSearcher(new DbConfig(),dbFile)
val datablock = search.btreeSearch(ip)
println(datablock)
//城市Id|国家|区域|省份|城市|ISP_
val db = datablock.getRegion
println(db)
val rep = db.replace("|",",")
val spl = rep.split(",")
val country = spl(0)
val province = spl(2)
val city = spl(3)
println(country)
println(province)
println(city)
}
}