栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot基础(11):IP查询城市

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot基础(11):IP查询城市

前言

最近各大平台都在显示用户的地址,本章介绍如何将ip转地址。

代码中使用到的库文件基于ipdb,官网地址:https://www.ipip.net/
本章代码已分享至Gitee:https://gitee.com/lengcz/springboot-ipdb.git

基于ipdb库的方式查询ip对应的地址
  1. 引入依赖

        
            net.ipip
            ipdb
            1.1.3
        
  1. 编写代码
package com.lengcz.springbootipdb.util;

import net.ipip.ipdb.City;
import java.io.IOException;


public class IpdbUtil {

    private static City city_DB;

    static {
        try {
            city_DB = new City(new IpdbUtil().getClass().getResource("/").getPath() + "ipipfree.ipdb");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    
    public static String[] find(String ip, String language) {
        try {
            return city_DB.find(ip, language);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
  1. 将ipipfree.db存入资源文件下(源码中已包含)
  2. 测试
 System.out.println(Arrays.toString(IpdbUtil.find("123.125.71.38", "CN")));


5. 启动项目,可以查到ip所对应的地址。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/863555.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号