首页 > 其他分享 >根据ip地址获得城市地址

根据ip地址获得城市地址

时间:2022-10-18 19:34:34浏览次数:73  
标签:ip2region ip 城市 db 查询 地址 import DbSearcher

转自:https://blog.csdn.net/m0_66167035/article/details/124628268

 

Ip2region是什么?
ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nodejs,golang,c#等查询绑定和Binary,B树,内存三种查询算法。

Ip2region特性
1.99.9%准确率

2.数据聚合了一些知名ip到地名查询提供商的数据,这些是他们官方的的准确率,经测试着实比经典的纯真IP定位准确一些。
3.ip2region的数据聚合自以下服务商的开放API或者数据(升级程序每秒请求次数2到4次):

4.ps:如果上述开放API或者数据都不给开放数据时ip2region将停止数据的更新服务。

标准化的数据格式
每条ip数据段都固定了格式:

城市Id|国家|区域|省份|城市|ISP

查询速度快
全部的查询客户端单次查询都在0.x毫秒级别,内置了三种查询算法:

1.memory算法:整个数据库全部载入内存,单次查询都在0.1x毫秒内,C语言的客户端单次查询在0.00x毫秒级别。
2.binary算法:基于二分查找,基于ip2region.db文件,不需要载入内存,单次查询在0.x毫秒级别。
3.b-tree算法:基于btree算法,基于ip2region.db文件,不需要载入内存,单词查询在0.x毫秒级别,比binary算法更快。
ps:任何客户端b-tree都比binary算法快,当然memory算法固然是最快的!

 

maven集成:

1.引入依赖:

<dependencies>
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
2.下载ip2region.db

3.将ip2region.db放入项目中的src/main/resource文件夹下

4.精彩的内容到了,就开始了代码

package utils;

import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;

public class IPUtil {

/**
* 根据IP地址获取城市
* @param ip
* 如果放在服务器读取不了,两种方式
* 1.可以在服务器上创建文件,ip2region.db这个放在文件里面,然后开始读取
* 2.可以整个配置文件,在配置配置目录,然后读取配置文件
* @return
*/
public static String getCityInfo(String ip) throws IOException {
URL url = IPUtil.class.getClassLoader().getResource("ip2region.db");
File file;
if (url != null) {
file = new File(url.getFile());
} else {
return null;
}
if (!file.exists()) {
System.out.println("Error: Invalid ip2region.db file, filePath:" + file.getPath());
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)) {
System.out.println("Error: Invalid ip address");
return null;
}
dataBlock = (DataBlock) method.invoke(searcher, ip);
return dataBlock.getRegion();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

}

package ceshi;

import utils.IPUtil;

import java.io.IOException;
import java.net.InetAddress;


public class CeShiIp {

public static void main(String[] args) throws IOException {
//获取IP地址
String hostAddress = InetAddress.getLocalHost().getHostAddress();
//工具类
String detail = IPUtil.getCityInfo(hostAddress);
System.out.println("IP地址:"+hostAddress+"地区:"+detail);
}
}

 

标签:ip2region,ip,城市,db,查询,地址,import,DbSearcher
From: https://www.cnblogs.com/konglong-cm/p/16803769.html

相关文章

  • IP数据报分片
    最大传送单元MTU链路层数据帧可封装数据的上限以太网的MTU是1500字节如果所传送的数据报超过最大MTU?采用++分片++的方式IP数据报格式++总长度单位是1B,片偏移单位是8B,......
  • [Typescript] 55. Medium - Reverse
    Implementthetypeversionof Array.reverseForexample:typea=Reverse<['a','b']>//['b','a']typeb=Reverse<['a','b','c']>//['c','b',......
  • IP数据报格式
    TCP/IP协议栈IP数据报格式......
  • ipv4/ipv6正则校验
    functionis_ipv4(str){ varreg=/^((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d).){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)$/; returnreg.test(str);}functionis_ipv6(str......
  • ipv4数据报格式及相关计算
       看起来一头雾水,其实这里主要考我们,ipv4数据报的格式理解。考点:1. ipv4的数据报格式      2. 进制转化      3. ipv4首部长度(单......
  • FFmpeg:FIFO + PIPE
    FIFO将某些内容流式传输到stdout管道,即使在临时故障(网络中断)的情况下也继续以实时速率处理流,并尝试无限期地每秒恢复流式传输。ffmpeg-re-stream_loop-1-ihttps://f......
  • 2022-10-18 uniapp h5端 通过腾讯提供的api并输入对应的经纬度 获取城市
    首先说明一下这是h5端,是的,他娘的h5端。然后先用uni.getLocation(我用的是wgs84)获取到经纬度,什么?你告诉我pc端无法获取,老是报什么网络错误的错误,连手机端也是这样??哦多茄~~......
  • python 打包离线环境 pip安装
    1、有网并且网速快时:生成的依赖文件pipfreeze>requirements.txt在新环境直接pipinstall-rrequirements.txtpipinstall-rrequestment.txt路径--target=安装目......
  • Rt3070芯片动态获取IP地址——station模式
    ———————————————————————————————————————主机操作系统:Centos6.7交叉编译器环境:arm-linux-gcc-4.5.4 开发板平台:FL2440 Linux......
  • 安装JDK以及eclipse
    一、下载安装JDK官方网址:https://www.oracle.com/java/technologies/downloads/如下图选择,然后打开安装,我的安装路径为D:\JAVA\JDK。 二、配置环境变量找到“计算机......