pom.xml

<dependency><groupId>com.maxmind.geoip2</groupId><artifactId>geoip2</artifactId><version>2.9.0</version>
</dependency>

示例

System.out.println(GeoIpUtils.getCountryName("113.116.217.190")); // China
System.out.println(GeoIpUtils.getCountryCode("113.116.217.190")); // CN

GeoIpUtils.java

import java.io.File;
import java.net.InetAddress;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CountryResponse;
import com.maxmind.geoip2.record.Country;/** * @Description: geoip工具类*/
public class GeoIpUtils {private final static Logger logger = LoggerFactory.getLogger(GeoIpUtils.class);private static DatabaseReader reader;private static DatabaseReader getReader(){try{if(reader == null){logger.warn("打开ip数据库");File database =  new File(GeoIpUtils.class.getClassLoader().getResource("geoip/GeoLite2-Country.mmdb").getFile()); // 附件下载百度云地址https://pan.baidu.com/s/1ENqTeCoMIWJMbh88nYU5ggreader = new DatabaseReader.Builder(database).build();}return reader;}catch(Exception e){return reader;}}/*** 根据ip获取国家对象,不存在则返回null* @param ip* @return*/public static Country getCountry(String ip){try{InetAddress ipAddress = InetAddress.getByName(ip);CountryResponse response = getReader().country(ipAddress);Country country = response.getCountry();return country;}catch(Exception e){return null;}}/*** 根据ip获取国家代码,不存在则返回null* @param ip* @return*/public static String getCountryCode(String ip){Country country = getCountry(ip);return country != null ? country.getIsoCode() : null;}/*** 根据ip获取国家名称,不存在则返回null* @param ip* @return*/public static String getCountryName(String ip){Country country = getCountry(ip);return country != null ? country.getName() : null;}
}

根据IP获取国家代码相关推荐

  1. PHP 通过ip获取国家及地址方法集合

    一.通过http请求淘宝和新浪的接口进行在线查询 优点:调用简单方便代码量少   缺点:在无网络的情况下无法查询. print_r(get_area('223.67.235.28')); functi ...

  2. 【java】使用ip获取 国家 省份 城市

    1.下载GeoLite2 City库 下载地址: http://dev.maxmind.com/geoip/geoip2/geolite2/ https://download.csdn.net/dow ...

  3. 根据IP获取城市代码

    1.淘宝IP地址库 官网API地址:http://ip.taobao.com/instructions.php 调用方式:http://ip.taobao.com/service/getIpInfo. ...

  4. golang 获取公网ip 内网ip 检测ip类型 校验ip区间 ip地址string和int转换 判断ip地区国家运营商

    目录 简要简介net包 什么是外网IP和内网IP? 获取公网ip 获取本地ip 判断是否是公网ip ip地址string转int ip地址int转string 判断ip地址区间 通过淘宝接口根据公网i ...

  5. 引用Geoip实现由IP地址获取国家城市等信息

    需求 当我们知道用户ip的情况下,需要统计用户所属的国家/省份/城市等信息. 这时可以用开源的Geoip2 避免重复造轮子. github地址:https://github.com/maxmind/G ...

  6. PHP 跟据用户IP获取所在国家高效解决方案(GEOIP)

    最近项目中有一个需求统计访客数据,为了保证效率.前端尽量轻量化,仅将访客原始请求信息不作任何处理直接写入消息队列.后端计划任务服务器监听消息队列,解析 user agent, ip 地址,处理结果写入 ...

  7. JAVA根据IP地址获取省份城市和经纬度(可获取国家名称 淘宝高德API如果是国外IP获取到的为空)

    所需jar包maven地址: <!-- 根据ip获取位置 --><dependency><groupId>com.maxmind.geoip2</groupI ...

  8. python 通过ip获取城市_python shell根据ip获取主机名代码示例

    这篇文章里我们主要分享了python中shell 根据 ip 获取 hostname 或根据 hostname 获取 ip的代码,具体介绍如下. 笔者有时候需要根据hostname获取ip 比如根据m ...

  9. 用 cctld工具创建带有国家代码的IP地址表

    用 cctld工具创建带有国家代码的IP地址表 cctld tools is creating IP addresses table with Country Code 项目地址 https://gi ...

  10. Nginx之IP国家代码ngx_http_geoip2_module模块简介和使用

    一.ngx_http_geoip2_module模块简介   ngx_http_geoip2_module模块使用预编译的MaxMind数据库创建变量,其值取决于客户端 IP 地址 ,通过此模块我们可 ...

最新文章

  1. 手把手教 | 使用Keras构造日文的神经网络语言模型(内附源码)
  2. SANS:2014年安全分析与安全智能调研报告
  3. ASP.NET Core身份认证服务框架IdentityServer4(2)-整体介绍
  4. :before 和 :after
  5. Java实现两个递增有序链表合并成一个递增有序链表和两个非递减有序链表合成一个非递增有序链表
  6. PHP中cURL错误号对照[转]
  7. python 碎片图像拼接_python实现图像拼接
  8. 【PostgreSQL-9.6.3】约束
  9. mozilla开源项目学习----智能指针(AutoPtrAutoArrayPtr)(上)
  10. js转换金钱为中文单位元、万元、亿元、万亿
  11. 8-@Pointcut( execution(* com.ctgu.controller.AccountController.transfer(..)) ) 拦截配置问题
  12. sql server2008 批量删除发生:查询处理器用尽了内部资源,无法生成查询计划。这种情况很少出现,只有在查询极其复杂或引用了大量表或分区时才会出现。请简化查询
  13. 问卷研究的五类分析思路模板
  14. CentOS7 安装php7.4
  15. 分布式存储HBASE原理学习
  16. 【一句日历】2019年2月
  17. 搜狗浏览器如何清除浏览器缓存--小白
  18. [FFmpeg] AVPacket 的使用记录(初始化、引用、解引用、释放)
  19. CONCATENATE示例
  20. 爬取(明星网)明星面部数据

热门文章

  1. 我的第一个游戏FoodieThebug完成之后的心得体会--子龙山人
  2. 基于 FFMPEG 的视频解码(libavcodec ,致敬雷霄骅)
  3. md5加密离线工具windwos,ubuntu
  4. 计算机游戏程序启动错误及其解决方法汇总
  5. 2007所做更改会影响共用模板normal
  6. 开4核后用哪个软件测试稳定性,测试CPU的稳定性的方法
  7. SQL学习03---数据库系统概念第6版第3章习题答案
  8. 数据库系统概念 第二章 习题答案
  9. 企业全链路数字化营销一站式解决方案
  10. imx6ull boot 启动