Java获取IP以及地址属地(全网无BUG)

点赞再看,养成习惯,大家好,我是辰兮!今天介绍如何获取访问人员的IP地址以及归属地(千万不要用这种方法做坏事噢!)


思路

通过此网站:获取IP网站即可获取访问者的IP,所以只需要通过对IP进行解析,获取到对应的归属地即可!

一、获取IP地址

首先我们创建一个IpUtils:

public class IpUtils {
}

获取IP的方法:访问上面那个网址然后解析得到IP地址

/**
     * 获取外网IP
     * @return
     */
    public static String getOutIP()  {
        String ip = "http://pv.sohu.com/cityjson?ie=utf-8";

        String inputLine = "";
        String read = "";
        String toIp="";
        try {
            URL url = new URL(ip);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            while ((read = in.readLine()) != null) {
                inputLine += read;
            }
            String ObjJson=inputLine.substring(inputLine.indexOf("=")+1,inputLine.length()-1);
//            System.out.println(ObjJson);
            JSONObject jsonObj= JSON.parseObject(ObjJson);
            toIp=jsonObj.getString("cip");
//            throw new Exception();
        } catch (Exception e) {
            toIp="";
        }
        return toIp;
    }

二、获取归属地

既然以及获取到IP地址了,这时候只需要解析IP地址得到归属地即可!

扫描二维码关注公众号,回复: 15673021 查看本文章

那么一个IP地址如何去解析得到归属地呢?

我们使用的是通过IP字典来解析,下面已经准备好了:

1、下载IP字典:

链接:https://pan.baidu.com/s/1xMj10JcBn89-tiyoXWYTfw?pwd=4y4f 
提取码:4y4f

然后下载完后放到src/main/resources目录下

2、加入ip2region依赖:

<dependency>
    <groupId>org.lionsoul</groupId>
    <artifactId>ip2region</artifactId>
    <version>1.7.2</version>
</dependency>

3、在创建的IpUtils里面加入getAddress()方法:

/**
     * 根据ip获取归属地
     * @param ip
     * @return
     */
    public static String getAddress(String ip) {
        URL url = HttpUtil.class.getClassLoader().getResource("ip2region.db");
        File file;
        if (url != null) {
            file = new File(url.getFile());
        } else {
            return null;
        }
        if (!file.exists()) {
            return null;
        }
        //查询算法
        int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
        //DbSearcher.BINARY_ALGORITHM //Binary
        //DbSearcher.MEMORY_ALGORITYM //Memory
        try {
            DbConfig config = new DbConfig();
            DbSearcher searcher = new DbSearcher(config, file.getPath());
            Method method;
            switch (algorithm){
                case DbSearcher.BTREE_ALGORITHM:
                    method = searcher.getClass().getMethod("btreeSearch", String.class);
                    break;
                case DbSearcher.BINARY_ALGORITHM:
                    method = searcher.getClass().getMethod("binarySearch", String.class);
                    break;
                case DbSearcher.MEMORY_ALGORITYM:
                    method = searcher.getClass().getMethod("memorySearch", String.class);
                    break;
                default:
                    return null;
            }
            DataBlock dataBlock;
            if (!Util.isIpAddress(ip)) {
                return null;
            }
            dataBlock  = (DataBlock) method.invoke(searcher, ip);
            return dataBlock.getRegion();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

三、测试

测试类:

    @Test
    void outIp(){
        String outIP = IpUtils.getOutIP();
        String address = IpUtils.getAddress(outIP);
        if (StringUtils.isEmpty(address)) {
            System.out.println("暂无归属地");
        } else {
            System.out.println("归属地为:" + address);
        }
    }

可以发现测试成功!!! 


总结

1、通过 http://pv.sohu.com/cityjson?ie=utf-8 网址获取IP数据

2、通过IP数据解析得到IP地址

3、通过IP字典解析IP地址得到归属地

猜你喜欢

转载自blog.csdn.net/ke2602060221/article/details/126483574