mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2026-05-11 14:22:08 +08:00
feat: 集成ip2region 实现离线IP地址定位库
This commit is contained in:
@@ -1,11 +1,7 @@
|
||||
package com.ruoyi.common.core.utils.ip;
|
||||
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import cn.hutool.http.HtmlUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.ruoyi.common.core.constant.Constants;
|
||||
import com.ruoyi.common.core.utils.JsonUtils;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -20,9 +16,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class AddressUtils {
|
||||
|
||||
// IP地址查询
|
||||
public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
|
||||
|
||||
// 未知地址
|
||||
public static final String UNKNOWN = "XX XX";
|
||||
|
||||
@@ -37,23 +30,8 @@ public class AddressUtils {
|
||||
return "内网IP";
|
||||
}
|
||||
// if (RuoYiConfig.isAddressEnabled()) {
|
||||
try {
|
||||
String rspStr = HttpUtil.createGet(IP_URL)
|
||||
.body("ip=" + ip + "&json=true", Constants.GBK)
|
||||
.execute()
|
||||
.body();
|
||||
if (StringUtils.isEmpty(rspStr)) {
|
||||
log.error("获取地理位置异常 {}", ip);
|
||||
return UNKNOWN;
|
||||
}
|
||||
Dict obj = JsonUtils.parseMap(rspStr);
|
||||
String region = obj.getStr("pro");
|
||||
String city = obj.getStr("city");
|
||||
return String.format("%s %s", region, city);
|
||||
} catch (Exception e) {
|
||||
log.error("获取地理位置异常 {}", ip);
|
||||
}
|
||||
return RegionUtils.getCityInfo(ip);
|
||||
// }
|
||||
return address;
|
||||
// return address;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ import java.io.InputStream;
|
||||
* @author lishuyan
|
||||
*/
|
||||
@Slf4j
|
||||
public class IpAddressUtil {
|
||||
public class RegionUtils {
|
||||
|
||||
private static final String LOCAL_REMOTE_HOST = "0:0:0:0:0:0:0:1";
|
||||
|
||||
@@ -45,7 +45,7 @@ public class IpAddressUtil {
|
||||
String fileName = "/ip2region.xdb";
|
||||
File existFile = FileUtil.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + fileName);
|
||||
if (!FileUtil.exist(existFile)) {
|
||||
InputStream resourceAsStream = IpAddressUtil.class.getResourceAsStream(fileName);
|
||||
InputStream resourceAsStream = RegionUtils.class.getResourceAsStream(fileName);
|
||||
if (ObjectUtil.isEmpty(resourceAsStream)) {
|
||||
throw new ServiceException(">>>>>>>> IpAddressUtil初始化失败,原因:IP地址库数据不存在!");
|
||||
}
|
||||
Reference in New Issue
Block a user