当前位置:网站首页>根据ip地址获取地理位置及坐标(离线方式)
根据ip地址获取地理位置及坐标(离线方式)
2022-07-30 05:42:00 【维之】
根据ip获取地理位置信息,不用http和webservice接口,减少请求时间。我们可以利用了GeoLite2 库,GeoLite2 数据库是一个免费的 IP 地理定位数据库,GeoLite2 Country 与 City 数据库在每月的第一个周二更新。GeoLite2 ASN 数据库的更新时间为每周二。
数据库下载 : https://dev.maxmind.com/zh-hans/geoip/geoip2/geolite2/#i
java文档: http://maxmind.github.io/GeoIP2-java/
以下是一个工具类demo
1. 首先将下载好的文件放置的resources 目录下,这利用的是city数据库
2. 工具类编写
/**
* ip地理坐标获取工具类
*/
public class Geoip2Client {
public static Map<String,Object> getGenIp(String ipAddr){
Map<String,Object> result = new HashMap<>();
try{
String dbPath = this.getClass().getClassLoader().getResource("GeoLite2-City.mmdb").getPath();
// 这是GeoIP2 或 GeoLite2 database 文件所在的位置 ,此处从项目resources路径下获取,当然也可以写成绝对路径
File database = new File(dbPath);
DatabaseReader reader = new DatabaseReader.Builder(database).withCache(new CHMCache()).build();
InetAddress ipAddress = InetAddress.getByName(ipAddr);
CityResponse response = reader.city(ipAddress);
Country country = response.getCountry();
Subdivision subdivision = response.getMostSpecificSubdivision();
City city = response.getCity();
Location location = response.getLocation();
result.put("lat",location.getLatitude());//纬度
result.put("long",location.getLongitude()); // 经度
result.put("country",country.getNames().get("zh-CN"));// 国家名
result.put("subdivision",subdivision.getNames().get("ja"));//省份
result.put("city",city.getNames().get("ja")); // 城市
} catch( Exception e){
e.printStackTrace();
}
return result;
}
}
边栏推荐
- Offensive and defensive world easy_web
- 信息安全必备神器之kali
- 浏览器缓存
- umi后台项目导航自定义icon问题
- volatility内存取证----命令演示
- DVWA installation tutorial (understand what you don't understand · in detail)
- uni-app: about custom components, easycom specs, uni_modules, etc.
- torch distributed training
- Blind injection, error injection, wide byte injection, stack injection study notes
- 最新Redistemplate配置及使用,附带操作工具类,测试类
猜你喜欢

目前主流浏览器以及对应的内核

node手写服务器实现访问index页面

TypeError The view function did not return a valid response. The function either returned None 的解决
Misc of CTF-image steganography
CTF之misc-内存分析(Volatility)

3 minutes to tell you how to become a hacker | Zero foundation to hacker introductory guide, you only need to master these five skills

JDBC一文搞懂

Competition WP in May

FastAPI Quick Start

c#下Web3合约空投、转账调用代码
随机推荐
使用Context API维护全局状态
uni-app:uni-icons的使用及如何自定义图标
c#下Web3合约空投、转账调用代码
JDBC一文搞懂
[Net Ding Cup 2020 Qinglong Group] AreUSerialz
npm run serve启动报错npm ERR Missing script “serve“
CTF之misc-日志分析
Detailed MySQL-Explain
TypeError The view function did not return a valid response. The function either returned None 的解决
最新Redistemplate配置及使用,附带操作工具类,测试类
Application Practice | Application Practice of Apache Doris in Baidu Intelligent Cloud Billing System
The operations engineer interview experience
awd——waf部署
Arrays工具类的使用
Misc of CTF - other types of steganography
史上超强最常用SQL语句大全
五月份比赛WP
umi后台项目导航自定义icon问题
Flink CDC 实现Postgres到MySQL流式加工传输案例
[网鼎杯 2020 青龙组]AreUSerialz