mirror of https://gitee.com/xiaonuobase/snowy
【更新】获取IP地址类更新
parent
66243186ce
commit
a2aa0b2597
|
@ -36,33 +36,33 @@ public class CommonIpAddressUtil {
|
||||||
|
|
||||||
private static final String LOCAL_REMOTE_HOST = "0:0:0:0:0:0:0:1";
|
private static final String LOCAL_REMOTE_HOST = "0:0:0:0:0:0:0:1";
|
||||||
|
|
||||||
private static final Searcher searcher;
|
private static final String IP2REGION_DB_PATH = "/ip2region.xdb";
|
||||||
|
|
||||||
static {
|
private static final Searcher searcher = initSearcher();
|
||||||
String fileName = "/ip2region.xdb";
|
|
||||||
File existFile = FileUtil.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + fileName);
|
/**
|
||||||
if(!FileUtil.exist(existFile)) {
|
* 私有构造函数防止实例化
|
||||||
InputStream resourceAsStream = CommonIpAddressUtil.class.getResourceAsStream(fileName);
|
*/
|
||||||
if(ObjectUtil.isEmpty(resourceAsStream)) {
|
private CommonIpAddressUtil() {
|
||||||
throw new CommonException("CommonIpAddressUtil初始化失败,原因:IP地址库数据不存在");
|
|
||||||
}
|
|
||||||
FileUtil.writeFromStream(resourceAsStream, existFile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String dbPath = existFile.getPath();
|
/**
|
||||||
|
* 初始化Searcher实例
|
||||||
|
*
|
||||||
|
* @return Searcher
|
||||||
|
*/
|
||||||
|
private static Searcher initSearcher() {
|
||||||
|
try {
|
||||||
|
final File dbFile = FileUtil.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + IP2REGION_DB_PATH);
|
||||||
|
if (!FileUtil.exist(dbFile)) {
|
||||||
|
try (InputStream inputStream = CommonIpAddressUtil.class.getResourceAsStream(IP2REGION_DB_PATH)) {
|
||||||
|
FileUtil.writeFromStream(inputStream, dbFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
// 1、从 dbPath 加载整个 xdb 到内存。
|
// 1、从 dbPath 加载整个 xdb 到内存。
|
||||||
byte[] cBuff;
|
final byte[] cBuff = Searcher.loadContentFromFile(dbFile.getPath());
|
||||||
try {
|
|
||||||
cBuff = Searcher.loadContentFromFile(dbPath);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error(">>> CommonIpAddressUtil初始化异常:", e);
|
|
||||||
throw new CommonException("CommonIpAddressUtil初始化异常");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
|
// 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
|
||||||
try {
|
return Searcher.newWithBuffer(cBuff);
|
||||||
searcher = Searcher.newWithBuffer(cBuff);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(">>> CommonIpAddressUtil初始化异常:", e);
|
log.error(">>> CommonIpAddressUtil初始化异常:", e);
|
||||||
throw new CommonException("CommonIpAddressUtil初始化异常");
|
throw new CommonException("CommonIpAddressUtil初始化异常");
|
||||||
|
@ -78,7 +78,7 @@ public class CommonIpAddressUtil {
|
||||||
public static String getIp(HttpServletRequest request) {
|
public static String getIp(HttpServletRequest request) {
|
||||||
if (ObjectUtil.isEmpty(request)) {
|
if (ObjectUtil.isEmpty(request)) {
|
||||||
return Ipv4Util.LOCAL_IP;
|
return Ipv4Util.LOCAL_IP;
|
||||||
} else {
|
}
|
||||||
try {
|
try {
|
||||||
String remoteHost = JakartaServletUtil.getClientIP(request);
|
String remoteHost = JakartaServletUtil.getClientIP(request);
|
||||||
return LOCAL_REMOTE_HOST.equals(remoteHost) ? Ipv4Util.LOCAL_IP : remoteHost;
|
return LOCAL_REMOTE_HOST.equals(remoteHost) ? Ipv4Util.LOCAL_IP : remoteHost;
|
||||||
|
@ -87,7 +87,6 @@ public class CommonIpAddressUtil {
|
||||||
return Ipv4Util.LOCAL_IP;
|
return Ipv4Util.LOCAL_IP;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据IP地址离线获取城市
|
* 根据IP地址离线获取城市
|
||||||
|
@ -97,9 +96,8 @@ public class CommonIpAddressUtil {
|
||||||
*/
|
*/
|
||||||
public static String getCityInfo(String ip) {
|
public static String getCityInfo(String ip) {
|
||||||
try {
|
try {
|
||||||
ip = ip.trim();
|
|
||||||
// 3、执行查询
|
// 3、执行查询
|
||||||
String region = searcher.search(ip);
|
String region = searcher.search(ip.trim());
|
||||||
return region.replace("0|", "").replace("|0", "");
|
return region.replace("0|", "").replace("|0", "");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return "未知";
|
return "未知";
|
||||||
|
|
Loading…
Reference in New Issue