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 Searcher searcher;
|
||||
private static final String IP2REGION_DB_PATH = "/ip2region.xdb";
|
||||
|
||||
static {
|
||||
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)) {
|
||||
throw new CommonException("CommonIpAddressUtil初始化失败,原因:IP地址库数据不存在");
|
||||
private static final Searcher searcher = initSearcher();
|
||||
|
||||
/**
|
||||
* 私有构造函数防止实例化
|
||||
*/
|
||||
private CommonIpAddressUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化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);
|
||||
}
|
||||
}
|
||||
FileUtil.writeFromStream(resourceAsStream, existFile);
|
||||
}
|
||||
|
||||
String dbPath = existFile.getPath();
|
||||
|
||||
// 1、从 dbPath 加载整个 xdb 到内存。
|
||||
byte[] cBuff;
|
||||
try {
|
||||
cBuff = Searcher.loadContentFromFile(dbPath);
|
||||
} catch (Exception e) {
|
||||
log.error(">>> CommonIpAddressUtil初始化异常:", e);
|
||||
throw new CommonException("CommonIpAddressUtil初始化异常");
|
||||
}
|
||||
|
||||
// 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
|
||||
try {
|
||||
searcher = Searcher.newWithBuffer(cBuff);
|
||||
// 1、从 dbPath 加载整个 xdb 到内存。
|
||||
final byte[] cBuff = Searcher.loadContentFromFile(dbFile.getPath());
|
||||
// 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
|
||||
return Searcher.newWithBuffer(cBuff);
|
||||
} catch (Exception e) {
|
||||
log.error(">>> CommonIpAddressUtil初始化异常:", e);
|
||||
throw new CommonException("CommonIpAddressUtil初始化异常");
|
||||
|
@ -78,14 +78,13 @@ public class CommonIpAddressUtil {
|
|||
public static String getIp(HttpServletRequest request) {
|
||||
if (ObjectUtil.isEmpty(request)) {
|
||||
return Ipv4Util.LOCAL_IP;
|
||||
} else {
|
||||
try {
|
||||
String remoteHost = JakartaServletUtil.getClientIP(request);
|
||||
return LOCAL_REMOTE_HOST.equals(remoteHost) ? Ipv4Util.LOCAL_IP : remoteHost;
|
||||
} catch (Exception e) {
|
||||
log.error(">>> 获取客户端ip异常:", e);
|
||||
return Ipv4Util.LOCAL_IP;
|
||||
}
|
||||
}
|
||||
try {
|
||||
String remoteHost = JakartaServletUtil.getClientIP(request);
|
||||
return LOCAL_REMOTE_HOST.equals(remoteHost) ? Ipv4Util.LOCAL_IP : remoteHost;
|
||||
} catch (Exception e) {
|
||||
log.error(">>> 获取客户端ip异常:", e);
|
||||
return Ipv4Util.LOCAL_IP;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,9 +96,8 @@ public class CommonIpAddressUtil {
|
|||
*/
|
||||
public static String getCityInfo(String ip) {
|
||||
try {
|
||||
ip = ip.trim();
|
||||
// 3、执行查询
|
||||
String region = searcher.search(ip);
|
||||
String region = searcher.search(ip.trim());
|
||||
return region.replace("0|", "").replace("|0", "");
|
||||
} catch (Exception e) {
|
||||
return "未知";
|
||||
|
|
Loading…
Reference in New Issue