geoip2解析IP所在省市【Java离线解析IP信息】

添加pom依赖

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

获取IP地址

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = "127.0.0.1";
        }
        if (ip.split(",").length > 1) {
            ip = ip.split(",")[0];
        }
        return ip;
    }

获取IP的信息(去官网下载GeoLite2-City.mmdb文件也可以到我的分享网盘中获取

        官方下载地址:【http://dev.maxmind.com/geoip/geoip2/geolite2/
我的链接:https://pan.baidu.com/s/1qYmoMBBy1MZS24PNRlJ_6g 
提取码:auic

public static String[] geoip2(String ip, String url){
        File file;
        // 创建 GeoLite2 数据库[GeoLite2-City.mmdb是关键文件(若该文件不存在则IP会解析失败)]
        file = new File(url + "GeoLite2-City.mmdb");
        if (!file.exists()) {
            logger.debug("Error: Invalid GeoLite2-City.mmdb file, filePath:" + file.getPath());
            return null;
        }
        // 读取数据库内容
        DatabaseReader reader = null;
        try {
            reader = new DatabaseReader.Builder(file).build();
            //解析IP地址
            InetAddress ipAddress = InetAddress.getByName(ip);
            // 获取查询结果
            CityResponse response = reader.city(ipAddress);
            // 获取国家信息
            String country = response.getCountry().getNames().get("zh-CN");
            // 获取省份
            String sheng = response.getMostSpecificSubdivision().getNames().get("zh-CN");
            //查询不到时保持与ip2region方式的返回结果一致
            if (sheng == null){
                sheng = "0";
            }
            // 获取城市
            String shi = response.getCity().getNames().get("zh-CN");
            if (shi == null){
                shi = "0";
            }
            String[] resu = {sheng,shi};
            return resu;
        } catch (IOException | GeoIp2Exception e) {
            e.printStackTrace();
            return null;
        }
    }

执行测试

    @PostMapping("/getResolveIP")
    public String[] getResolveIP(HttpServletRequest request){
        //获取用户IP
        String ip = getIpAddr(request);
        //解析IP信息
        return geoip2(ip,"自己的 GeoLite2-City.mmdb 文件路径");
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值