GeoIP获取IP的所在地区
1.下载文件
官网地址:https://dev.maxmind.com/?lang=en
最下方从Products→GeoLite2 Free Geolocation Data 进入
进入之后,按照登陆→生成通行证→下载文件步骤
注册和生成通行证这里略过,点击Download Files
下载文件,
分别包含:ASN版本、Country版本、City版本,每个都包涵.mmdb
和.csv
格式
ASN.mmdb
用于产看IP地址的拥有者(7M左右)
ASN.csv
用于产看IP地址的拥有者(7M左右)
Country.mmdb
精确到国家(4M左右)
Country.csv
精确到国家(4M左右)
City.mmdb
精确到城市(大小70M左右)
City.csv
精确到城市(大小70M左右)
这里我选择City.mmdb
,下载
2.引入依赖
<dependency><groupId>com.maxmind.db</groupId><artifactId>maxmind-db</artifactId><version>2.0.0</version></dependency><dependency><groupId>com.maxmind.geoip2</groupId><artifactId>geoip2</artifactId><version>2.16.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.11.2</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.30</version></dependency>
3.引入工具类
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.AddressNotFoundException;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.tiens.lark.common.exception.BusinessException;
import org.apache.commons.lang.StringUtils;import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.Map;public class GeoIPReader {private static DatabaseReader databaseReader;static {try {InputStream inputStream = GeoIPReader.class.getClassLoader().getResourceAsStream("GeoLite2-City.mmdb");databaseReader = new DatabaseReader.Builder(inputStream).build();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) throws IOException, GeoIp2Exception {String addeassByIP = getCountryAndRegionByIP("1.203.160.07");System.out.println("addeassByIP = " + addeassByIP);}public static String getCountryAndRegionByIP(String ip) throws IOException, GeoIp2Exception {String ipRegex = "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";if(!ip.matches(ipRegex)){throw new BusinessException("ip 地址不合法");}String[] countryAndRegion = getCountryAndRegion(ip,"zh-CN");String country = countryAndRegion[0];String region = countryAndRegion[1];return StringUtils.isBlank(region) ? country : country + "-" + region;}/*** 根据ip获取国家和地区* @param ip IP地址* @param language 语言,包含:{de=China, ru=Китай, pt-BR=China, ja=中国, en=China, fr=Chine, zh-CN=中国, es=China}* @return* @throws IOException* @throws GeoIp2Exception*/private static String[] getCountryAndRegion(String ip, String language) throws IOException, GeoIp2Exception {try {InetAddress ipAddress = InetAddress.getByName(ip);CityResponse response = databaseReader.city(ipAddress);Map<String, String> countryNames = response.getCountry().getNames();String country = countryNames.get(language);Map<String, String> subNames = response.getMostSpecificSubdivision().getNames();String region = subNames.get(language);return new String[]{country, region};} catch (AddressNotFoundException e) {return new String[]{null, null};}}
}
4.本地测试
打印成功~
5.写在最后
在自测的过程中,发现了很多ip都是只能显示国家,没有地区,由于我当前下载的是70MB的city.mmdb,但是里面囊括的ip还是太少,截止到2023年3月21日,里面收录的ip总共有将近380w个,而中国ip仅有19600余个,所以选择这个免费版还是有很大局限性,另外如果需要部署到公司的线上环境,70MB的内存还是比较损耗资源的,这里就推荐大家下载CSV格式,自行导入mysql,以下是mysql的相关脚本代码
CREATE TABLE `GeoLite2_IPv4` (`network` varchar(20) NOT NULL COMMENT 'IP',`geoname_id` varchar(20) DEFAULT '' COMMENT '城市编号',`registered_country_geoname_id` varchar(20) DEFAULT '' COMMENT '国家编号',PRIMARY KEY (`network`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='IP城市表';CREATE TABLE `GeoLite2_City_CN` (`geoname_id` varchar(20) NOT NULL COMMENT '城市编号',`country_iso_code` varchar(20) NOT NULL COMMENT '国家编码',`country_name` varchar(20) DEFAULT '' COMMENT '国家名称',`subdivision_1_name` varchar(20) NOT NULL COMMENT '省份名称',`city_name` varchar(20) DEFAULT '' COMMENT '城市名称',`time_zone` varchar(20) NOT NULL COMMENT '时区',PRIMARY KEY (`geoname_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='城市表';
将下载好的文件导入到对应mysql表中即可
GeoIP获取IP的所在地区相关推荐
- QQ小游戏、微信小游戏获取ip地址,并通过ip得到所在地区
QQ小游戏.微信小游戏获取用户的ip地址,并通过ip得到所在地区 问题解决了就点赞,还有疑问就留言或是私我,臭弟弟 记得要把合法域名加进去,或者编译的时候不校验合法域名等,不然会报错 如果是后端屏蔽, ...
- php 选择地区 查找,地区编码查询_php根据ip查询所在地区的代码
摘要 腾兴网为您分享:php根据ip查询所在地区的代码,中信金通,盈益云,熊猫视频,湘税社保等软件知识,以及游戏通用窗口化工具,史诗战争补丁,荣威车友会,开机引导,兴化农商行,修改手机mac地址,问道 ...
- Python通过GeoIP获取IP信息(国家、城市、经纬度等)
IP地址信息是非常重要的情报信息,通过IP可以定位到该IP所在的国家.城市.经纬度等. 获取IP信息的方式有很多,很多服务商都提供了相应的地址库或API接口服务. 如国内的ipip.net,国外的ip ...
- PHP获取IP地址所在的地理位置
PHP获取IP地址所在的地理位置 时间:2011-04-02 16:19来源:三知开发网 作者:许亮 点击: 787 次 http://www.sunchis.com/html/php/phpsour ...
- php 获取ip地址所在的区域
/** * get_ip_lookup 获取ip地址所在的区域 * @param null $ip * @return bool|mixed */ function get_ip_lookup($ip ...
- IP归属PHP调用,解析php根据ip查询所在地区(非常有用,赶集网就用到)
dat文件,关于ip对应地区的信息文件 qqwry.dat文件 网上自己下载 class类文件,解析qqwry.data文件的 IpLocation.php文件 代码如下: class IpLocat ...
- PHP 获取 IP 地址所在的地理位置信息/城市
源码:https://github.com/hacfins/thinkphp6-api/blob/master/app/common/third/MapService.php 类似的博客很多,五花八门 ...
- phpip地址查询_PHP获取IP地址所在的地理位置
/* *根据新浪IP查询接口获取IP所在地 */ function getIPLoc_sina($queryIP){ $url = 'http://int.dpool.sina.com.cn/iplo ...
- 通过GeoIP获取ip所属地 (国家,城市,时区,邮编,经纬度等)
来源: http://www.phpandstuff.com/articles/geoip-country-lookup-with-php GeoIP + PHP <?php//计时开始func ...
最新文章
- [导入]Nhibernate引入自定义Membership和Role
- laravel mysql like_Laravel数据库查询中对 like 的值进行转义
- 2.标签CCLabelTTF,CCLabelAtlas,CCLabelBMFont
- PHP接入支付宝手机网站支付、移动支付接口
- linux 命令/目录 名称 英文单词 缩写 助记
- 在sqlserver sql语句中查找速度快
- numeric库函数——accumulate函数
- 使用arcgis、matlab与R语言GD包进行地理探测器 批量运行,并导出探测结果
- esxi安装系统ndows,ESXI 安装 Windows Server 2012过程
- 基于Unity构建机器人的数字孪生平台系列2—四旋翼无人机三维模型
- 领导周末喊程序员修错误,程序员霸气回应:在下卖艺不是卖身!
- Powermill汽车件模具五轴数控CNC编程视频教程
- uniapp微信小程序项目-优购商城
- html如何添加web字体,html中字体如何实现加粗(方法介绍)_WEB前端开发,html,字体加粗...
- sap客户信贷_SAP信贷控制功能与配置详解
- 为何用户体验无法被设计,如何为用户体验设计
- 【点的定位】Save the Students! UVALive - 5984
- 详解vue生命周期(重点!!)
- arp同网段一条就能让对方断网攻击命令
- 使用yanderify搞个图片转视频(让大头照动起来)