Java如何根據(jù)根據(jù)IP地址獲取省市
本文介紹了兩種在Java中獲取IP地址對(duì)應(yīng)地理位置信息的方法。第一種是使用ip2region庫,它提供毫秒級(jí)查詢且數(shù)據(jù)庫小巧,適合快速定位。第二種是利用GeoIP庫,基于GeoLite數(shù)據(jù)庫,可以獲取到城市級(jí)別的信息。兩種方法各有特點(diǎn),適用于不同的場景需求。在選擇方案時(shí),需要考慮精度、速度和維護(hù)成本等因素。
方案一
最近要做一個(gè)埋點(diǎn)的功能,需求里要求記錄用戶登錄的IP和地點(diǎn),打算利用ip2region.db實(shí)現(xiàn)
首先下載ip2region.db
地址:下載地址
導(dǎo)入依賴
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>1.7.2</version>
</dependency>
工具類
public class IPUtil {
/**
* 獲取IP地址
* @param request
* @return
*/
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;
}
/**
* 根據(jù)IP地址獲取城市
* @param ip
* @return
*/
public static String getCityInfo(String ip) throws IOException {
URL url = IPUtil.class.getResource("/ip2region.db");
File file;
file = new File("/app/project/croot_rims/package/webserver/ip2region.db");
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;
}
}
具體使用
String detail = IpUtils.getCityInfo("113.115.178.39");
方案二
在Java中,我們可以使用第三方庫,例如GeoIP來獲取IP地址所對(duì)應(yīng)的地理位置信息。這個(gè)庫是基于MaxMind的GeoLite數(shù)據(jù)庫,它包含了大量的IP地址和相應(yīng)的地理位置信息。
首先,你需要下載GeoLite數(shù)據(jù)庫(這是一個(gè).dat文件),然后將其放在你的項(xiàng)目的某個(gè)位置。接下來,你可以編寫一個(gè)函數(shù)來查詢這個(gè)數(shù)據(jù)庫并獲取IP地址的地理位置信息。
以下是一個(gè)簡單的示例:
public class GeoIPTest {
public static void main(String[] args) throws IOException, GeoIp2Exception {
String ipAddress = "你的IP地址"; // 替換成你要查詢的IP地址
File database = new File("你的數(shù)據(jù)庫文件路徑"); // 替換成你的GeoLite數(shù)據(jù)庫文件路徑
DatabaseReader dbReader = new DatabaseReader.Builder(database).build();
InetAddress ip = InetAddress.getByName(ipAddress);
CityResponse response = dbReader.city(ip);
City city = response.getCity();
System.out.println(city.getName()); // 輸出城市名
}
}
注意:這個(gè)例子只能獲取到城市級(jí)別的信息,如果你需要更詳細(xì)的信息(比如具體的經(jīng)緯度),你可以查看CityResponse對(duì)象的其他方法。
最后,別忘了將這個(gè)庫添加到你的項(xiàng)目依賴中。如果你使用的是Maven,你可以在你的pom.xml文件中添加以下依賴:
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.12.0</version> <!-- 請(qǐng)檢查最新版本 -->
</dependency>到此這篇關(guān)于Java如何根據(jù)根據(jù)IP地址獲取省市的文章就介紹到這了,更多相關(guān)Java IP地址獲取省市內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中的notyfy()和notifyAll()的本質(zhì)區(qū)別
很多朋友對(duì)java中的notyfy()和notifyAll()的本質(zhì)區(qū)別不了解,今天小編抽空給大家整理一篇教程關(guān)于Java中的notyfy()和notifyAll()的本質(zhì)區(qū)別,需要的朋友參考下吧2017-02-02
PostMan傳@RequestParam修飾的數(shù)組方式
這篇文章主要介紹了PostMan傳@RequestParam修飾的數(shù)組方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring Security角色繼承實(shí)現(xiàn)過程解析
這篇文章主要介紹了Spring Security角色繼承實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Sharding-jdbc報(bào)錯(cuò):Missing the data source
在使用MyBatis-plus進(jìn)行數(shù)據(jù)操作時(shí),新增Order實(shí)體屬性后,出現(xiàn)了數(shù)據(jù)源缺失的提示錯(cuò)誤,原因是因?yàn)閡serId屬性值使用了隨機(jī)函數(shù)生成的Long值,這與sharding-jdbc的路由規(guī)則計(jì)算不匹配,導(dǎo)致無法找到正確的數(shù)據(jù)源,通過調(diào)整userId生成邏輯2024-11-11
Redisson RedLock紅鎖加鎖實(shí)現(xiàn)過程及原理
本文主要介紹了Redis中Redisson紅鎖(Redlock)使用原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
async-excel實(shí)現(xiàn)多sheet異步導(dǎo)出方法詳解
這篇文章主要介紹了async-excel實(shí)現(xiàn)多sheet異步導(dǎo)出方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12

