JAVA根據(jù)ip地址獲取歸屬地的實(shí)現(xiàn)方法
IP獲取歸屬地
1.通過(guò)地址庫(kù)獲取
如果使用API接口獲取,可能會(huì)出現(xiàn)服務(wù)掛了,或者服務(wù)地址不提供服務(wù)了等問(wèn)題。而采用本地地址庫(kù)就沒(méi)有這些問(wèn)題。
本文采用離線IP地址定位庫(kù) Ip2region,Ip2region是一個(gè)離線IP地址定位庫(kù),微秒的查詢時(shí)間:
實(shí)現(xiàn)步驟:
訪問(wèn)官網(wǎng)github地址:https://github.com/lionsoul2014/ip2region

找到data目錄下的:ip2region.xdb文件下載下來(lái)

把ip2region.xdb文件放在resources目錄下

在模塊中引入maven依賴
<dependency> <groupId>org.lionsoul</groupId> <artifactId>ip2region</artifactId> <version>2.6.5</version> </dependency>
獲取歸屬地:
private Searcher searcher;
@Override
public String getIpAddress(String ip){
if ("127.0.0.1".equals(ip) || ip.startsWith("192.168")) {
return "|||局域網(wǎng)ip";
}
if (searcher == null) {
try {
File file = ResourceUtils.getFile("classpath:db/data_ip2region.xdb");
String dbPath = file.getPath();
searcher = Searcher.newWithFileOnly(dbPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
String region = null;
String errorMessage = null;
try {
region = searcher.search(ip);
} catch (Exception e) {
errorMessage = e.getMessage();
if (errorMessage != null && errorMessage.length() > 256) {
errorMessage = errorMessage.substring(0, 256);
}
e.printStackTrace();
}
// 輸出 region
System.out.println(region);
return region;
}
這里Searcher引用的是倉(cāng)庫(kù)里面的檢索方法,到這里就完成了可以獲取到ip對(duì)應(yīng)的歸屬地。
調(diào)用方法后運(yùn)行結(jié)果如下

注意?。。。?本地是沒(méi)問(wèn)題的 如果打成jar包放在linux服務(wù)器上會(huì)讀取不到
解決辦法:
public String getIpCity(String ip) {
if ("127.0.0.1".equals(ip) || ip.startsWith("192.168")) {
return "|||局域網(wǎng)ip";
}
if (searcher == null) {
try {
//本地環(huán)境需要加上 classpath:
// File file = ResourceUtils.getFile("db/data_ip2region.xdb");
// String dbPath = file.getPath();
// searcher = Searcher.newWithFileOnly(dbPath);
//這里通過(guò)流獲取 解決jar包無(wú)法讀取文件問(wèn)題
ResponseEntity<byte[]> test = test("db/data_ip2region.xdb");
searcher = Searcher.newWithBuffer(test.getBody());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
String region = null;
String errorMessage = null;
try {
region = searcher.search(ip);
} catch (Exception e) {
errorMessage = e.getMessage();
if (errorMessage != null && errorMessage.length() > 256) {
errorMessage = errorMessage.substring(0, 256);
}
e.printStackTrace();
}
// 輸出 region
return region;
}
public static ResponseEntity<byte[]> test(String templateName) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(templateName);
String filename = classPathResource.getFilename();
@Cleanup InputStream inputStream = classPathResource.getInputStream();
byte[] bytes = FileCopyUtils.copyToByteArray(inputStream);
String fileName = new String(filename.getBytes("UTF-8"), "iso-8859-1");// 為了解決中文名稱亂碼問(wèn)題
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", fileName);
return new ResponseEntity<>(bytes, headers, HttpStatus.CREATED);
}至于什么是Ip2region 官網(wǎng)上面有介紹這里就不多介紹了
到此這篇關(guān)于JAVA根據(jù)ip地址獲取歸屬地的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)JAVA ip地址獲取歸屬地內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成SAP的過(guò)程及本地IDEA啟動(dòng)和Windows服務(wù)器部署
本文給大家介紹SpringBoot集成SAP的過(guò)程及本地IDEA啟動(dòng)和Windows服務(wù)器部署的相關(guān)知識(shí),本文給大家介紹的詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-07-07
Java中的clone()和Cloneable接口實(shí)例
這篇文章主要介紹了Java中的clone()和Cloneable接口實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java SpringBoot詳解集成以及配置Swagger流程
Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)??傮w目標(biāo)是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來(lái)更新。文件的方法,參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許API來(lái)始終保持同步2021-10-10
關(guān)于SpringBoot接收json格式的Demo案例
這篇文章主要介紹了關(guān)于SpringBoot接收json格式的Demo案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Java實(shí)戰(zhàn)項(xiàng)目之校園跑腿管理系統(tǒng)的實(shí)現(xiàn)
只有理論是不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+vue+maven+elementui+mysql實(shí)現(xiàn)一個(gè)校園跑腿管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2022-01-01

