JAVA根據ip地址獲取歸屬地的實現方法
IP獲取歸屬地
1.通過地址庫獲取
如果使用API接口獲取,可能會出現服務掛了,或者服務地址不提供服務了等問題。而采用本地地址庫就沒有這些問題。
本文采用離線IP地址定位庫 Ip2region,Ip2region是一個離線IP地址定位庫,微秒的查詢時間:
實現步驟:
訪問官網github地址:https://github.com/lionsoul2014/ip2region

找到data目錄下的:ip2region.xdb文件下載下來

把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 "|||局域網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引用的是倉庫里面的檢索方法,到這里就完成了可以獲取到ip對應的歸屬地。
調用方法后運行結果如下

注意?。。?! 本地是沒問題的 如果打成jar包放在linux服務器上會讀取不到
解決辦法:
public String getIpCity(String ip) {
if ("127.0.0.1".equals(ip) || ip.startsWith("192.168")) {
return "|||局域網ip";
}
if (searcher == null) {
try {
//本地環(huán)境需要加上 classpath:
// File file = ResourceUtils.getFile("db/data_ip2region.xdb");
// String dbPath = file.getPath();
// searcher = Searcher.newWithFileOnly(dbPath);
//這里通過流獲取 解決jar包無法讀取文件問題
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");// 為了解決中文名稱亂碼問題
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", fileName);
return new ResponseEntity<>(bytes, headers, HttpStatus.CREATED);
}至于什么是Ip2region 官網上面有介紹這里就不多介紹了
到此這篇關于JAVA根據ip地址獲取歸屬地的實現方法的文章就介紹到這了,更多相關JAVA ip地址獲取歸屬地內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Cloud 的 Hystrix.功能及實踐詳解
這篇文章主要介紹了Spring Cloud 的 Hystrix.功能及實踐詳解,Hystrix 具備服務降級、服務熔斷、線程和信號隔離、請求緩存、請求合并以及服務監(jiān)控等強大功能,需要的朋友可以參考下2019-07-07
Apache?Commons?BeanUtils:?JavaBean操作方法
這篇文章主要介紹了Apache?Commons?BeanUtils:?JavaBean操作的藝術,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
springboot中使用ConstraintValidatorContext驗證兩個字段內容相同
開發(fā)修改密碼功能時,通過ConstraintValidator校驗新密碼和確認新密碼的一致性,首先定義Matches注解和DTO對象,然后創(chuàng)建MatchesValidator類實現驗證邏輯,對springboot驗證字段內容相同問題感興趣的朋友一起看看吧2024-10-10

