本文主要是介绍java 根据ip获取到城市 GeoLite2-City.mmdb,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本文可解决 根据ip定位获取不到问题,提供多种方式仅供参考:
1.选型
1.1 实现方式
Java可以实现IP地址解析和省市区信息查询,但是需要借助一些外部数据源或数据库来实现。常用的方法有以下几种:
1.1.1 本地文件解析
可以通过下载最新的IP地址段数据文件,然后在程序中解析该文件来获取IP地址对应的省市区等信息。不过这种方法需要手动维护IP库文件,并且更新和查询速度可能较慢,不适合高并发或大规模的应用场景。
1.1.2 数据库查询
可以将IP地址段数据存储在数据库中,然后根据用户请求的IP地址来查询对应的省市区等信息。常用的数据库有MySQL、Oracle、SQL Server等。这种方法可以提高查询效率和精度,并支持更灵活的查询方式,但是需要考虑数据库的设计和优化问题。
1.1.3 第三方API代理
使用第三方IP查询API代理来实现纯Java的IP地址解析和省市区信息查询。比如通过调用百度地图API、高德地图API等第三方服务来获取IP地址对应的位置信息。这种方法可以免去自行搭建IP数据库的麻烦,同时也能保证查询结果的准确性和实时性,但需要考虑API使用频率和费用等问题。
1.2 常用ip定位库
1.2.1 Ip2region Ip2region
快速实现 IP地址解析 - 掘金
Ip2region:Ip2region是一个基于Java的IP地址定位库,提供了快速而准确的IP查询服务。它将全球IP地址划分为多级区域,可以根据IP地址获取对应的省市区信息、运营商等详细信息。
参考:连接
1.2.2 GeoLite2
GeoLite2:一款免费的IP地址库,由MaxMind公司开发和维护,提供高精度的IP地址定位服务。可通过下载数据库文件或使用API来查询IP地址对应的位置信息。
MaxMind GeoIP2 Java API
GeoLite2 Java根据IP获得城市、经纬度 | 言曌博客
java 根据ip获取到城市_mob64ca12d4a164的技术博客_51CTO博客
1.2.3 IP2Location
IP2Location:一款商业化的IP地址库,提供全球范围内的IP地址定位服务,支持IPv4/IPv6地址解析。可通过订阅服务或购买数据库文件来使用。
1.2.4 ipapi
ipapi:一款云端IP地址查询API,提供高效和准确的IP地址定位服务。支持JSON/XML格式的返回数据,可根据用户需求选择不同的套餐和服务计划。
1.2.5 QQWry
QQWry:国内最广泛使用的IP地址库之一,提供了IP地址到省市区县、运营商等信息的映射。可通过下载最新版的dat文件或使用API来实现IP地址定位。
1.2.6 其他
国内还有很多其他的定位库,如淘宝、新浪、搜狐等ip库,有兴趣的朋友自己了解下。
1.3 选择
1.3 .1综合下面几种情况考虑最后选择了 GeoLite2
GeoLite2-City.mmdb 下载连接1
GeoLite2-City.mmdb 下载连接2
GeoLite2-City.mmdb 下载连接3
import javax.servlet.http.HttpServletRequest;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
/*** @description:* @author: LAN*/
public class Ttest {public String getUserIpAddress(HttpServletRequest request) {String ipAddress = request.getHeader("X-Forwarded-For");if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {ipAddress = request.getHeader("Proxy-Client-IP");}if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {ipAddress = request.getHeader("WL-Proxy-Client-IP");}if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {ipAddress = request.getRemoteAddr();}return ipAddress;}public CityResponse getInfo(String ip) throws IOException, GeoIp2Exception {File database = new File("C:\\Users\\lenovo\\Desktop\\test\\GeoLite2-City.mmdb"); // GeoLite2数据库文件路径DatabaseReader reader = new DatabaseReader.Builder(database).build();InetAddress ipAddress = InetAddress.getByName(ip);CityResponse response = reader.city(ipAddress);return response;}public String getCityInfo(String ipAddress) {CityResponse cityResponse;try {cityResponse = getInfo(ipAddress);String cityName = cityResponse.getCity().getName(); // '英文城市'String countryName = cityResponse.getCountry().getName(); // '英文国家'String countryNameZ = cityResponse.getCountry().getNames().get("zh-CN"); // '国家'String subdivisionNameZ = cityResponse.getLeastSpecificSubdivision().getNames().get("zh-CN"); // '省份'String cityNameZ = cityResponse.getCity().getNames().get("zh-CN"); // '城市'System.out.println("所在国家:" + countryNameZ+",您当前所在省份:" + subdivisionNameZ +",您当前所在城市:" + cityNameZ);return "您当前所在城市:" + cityName + ",所在国家:" + countryName;} catch (IOException | GeoIp2Exception e) {return "获取城市信息失败:" + e.getMessage();}}public String getCityInfo(HttpServletRequest request) {String ipAddress = getUserIpAddress(request);CityResponse cityResponse;String cityName =null;String countryName =null;try {cityResponse = getInfo(ipAddress);cityResponse.getCity().getName();cityResponse.getCountry().getName();} catch (IOException e) {return "获取城市信息失败:" + e.getMessage();} catch (GeoIp2Exception e) {e.printStackTrace();}return "您当前所在城市:" + cityName + ",所在国家:" + countryName;}public static void main(String[] args) {Ttest t=new Ttest();System.out.println(t.getCityInfo("111.18.134.223"));}
1.3 .1 通过网络连接进行提取 ipshudi
import cn.hutool.http.HttpRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;/*** @description:* @author: LAN*/
public class tttttest {public static void main(String[] args) {String html = HttpRequest.get("https://www.ipshudi.com/111.18.134.223.htm").execute().body();String pattern= "<a.*? rel=\"nofollow\" target=\"_blank\">(.*?)</a>";Pattern regex = Pattern.compile(pattern);Matcher matcher = regex.matcher(html);while (matcher.find()) {System.out.println("matcher.group(0) "+matcher.group(0));//得到第0组——整个匹配System.out.println("matcher.group(1) "+matcher.group(1));//得到第一组与第一个括号中对应中值匹配}System.out.println("*****");}
}
https://qifu-api.baidubce.com/ip/geo/v1/district?ip=#{ip}&type=0
https://ip.cn/api/index?ip=#{IP}&type=0
http://whois.pconline.com.cn/?ip=#{IP}
https://www.ipshudi.com/#{IP}.htm
通过ip获取所在地理位置、通过手机号获取归属地_根据ip获取所在城市-CSDN博客
这篇关于java 根据ip获取到城市 GeoLite2-City.mmdb的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!