字典类型加上组织字段
This commit is contained in:
@@ -65,27 +65,67 @@ public class IpUtil {
|
||||
public static IpLocation getLocation(String ip) {
|
||||
IpLocation location = new IpLocation();
|
||||
location.setIp(ip);
|
||||
try (InputStream inputStream = IpUtils.class.getResourceAsStream("/ip2region.xdb");) {
|
||||
if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip.trim()))
|
||||
{
|
||||
return location;
|
||||
}
|
||||
String ipv4 = ip.trim();
|
||||
if (ipv4.contains(","))
|
||||
{
|
||||
ipv4 = ipv4.substring(0, ipv4.indexOf(',')).trim();
|
||||
}
|
||||
// ip2region xdb 对 IPv6 / 非法串可能抛 ArrayIndexOutOfBoundsException,仅对 IPv4 做库查询
|
||||
if (!isLikelyIpv4(ipv4))
|
||||
{
|
||||
return location;
|
||||
}
|
||||
try (InputStream inputStream = IpUtils.class.getResourceAsStream("/ip2region.xdb"))
|
||||
{
|
||||
if (inputStream == null)
|
||||
{
|
||||
log.warn("ip2region.xdb 未找到,跳过 IP 归属解析");
|
||||
return location;
|
||||
}
|
||||
byte[] bytes = IoUtil.readBytes(inputStream);
|
||||
Searcher searcher = Searcher.newWithBuffer(bytes);
|
||||
String region = searcher.search(ip);
|
||||
if (StrUtil.isAllNotBlank(region)) {
|
||||
// xdb返回格式 国家|区域|省份|城市|ISP,
|
||||
// 只有中国的数据绝大部分精确到了城市,其他国家部分数据只能定位到国家,后前的选项全部是0
|
||||
String[] result = region.split("\\|");
|
||||
location.setCountry(ZERO.equals(result[0]) ? StrUtil.EMPTY : result[0]);
|
||||
location.setProvince(ZERO.equals(result[2]) ? StrUtil.EMPTY : result[2]);
|
||||
location.setCity(ZERO.equals(result[3]) ? StrUtil.EMPTY : result[3]);
|
||||
location.setIsp(ZERO.equals(result[4]) ? StrUtil.EMPTY : result[4]);
|
||||
try
|
||||
{
|
||||
String region = searcher.search(ipv4);
|
||||
if (StrUtil.isNotBlank(region))
|
||||
{
|
||||
// xdb返回格式 国家|区域|省份|城市|ISP,
|
||||
String[] result = region.split("\\|");
|
||||
if (result.length >= 5)
|
||||
{
|
||||
location.setCountry(ZERO.equals(result[0]) ? StrUtil.EMPTY : result[0]);
|
||||
location.setProvince(ZERO.equals(result[2]) ? StrUtil.EMPTY : result[2]);
|
||||
location.setCity(ZERO.equals(result[3]) ? StrUtil.EMPTY : result[3]);
|
||||
location.setIsp(ZERO.equals(result[4]) ? StrUtil.EMPTY : result[4]);
|
||||
}
|
||||
}
|
||||
}
|
||||
searcher.close();
|
||||
} catch (Exception e) {
|
||||
log.error("ip地址解析异常,error:", e);
|
||||
finally
|
||||
{
|
||||
searcher.close();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.warn("ip地址解析失败,ip={}, msg={}", ipv4, e.getMessage());
|
||||
return location;
|
||||
}
|
||||
return location;
|
||||
}
|
||||
|
||||
private static boolean isLikelyIpv4(String ip)
|
||||
{
|
||||
if (ip == null || ip.contains(":"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return ip.chars().filter(ch -> ch == '.').count() == 3L;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据iP获取归属地信息 - 拼接
|
||||
*
|
||||
@@ -93,7 +133,7 @@ public class IpUtil {
|
||||
*/
|
||||
public static String getLocationSplicing(String ip) {
|
||||
IpLocation location = getLocation(ip);
|
||||
return location.getProvince()+location.getCity();
|
||||
return StrUtil.nullToEmpty(location.getProvince()) + StrUtil.nullToEmpty(location.getCity());
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Reference in New Issue
Block a user