From 0279a86e565cbb6501a2338b26a38524b5e20564 Mon Sep 17 00:00:00 2001 From: xiongxiaoyang <1179705413@qq.com> Date: Tue, 14 Oct 2025 10:17:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=AD=E5=9B=BD?= =?UTF-8?q?=E9=83=A8=E5=88=86=20IP=20=E6=97=A0=E7=9C=81=E4=BB=BD=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=AF=BC=E8=87=B4=E5=9C=B0=E7=90=86=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E4=B8=BA=E2=80=9C0=E2=80=9D=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 部分中国 IP(如 46.248.24.0-46.248.25.255)缺少省份信息,返回值为“0”。 现优化为直接显示国家名称“中国”。 --- .../novel/service/impl/IpLocationServiceImpl.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/novel-front/src/main/java/com/java2nb/novel/service/impl/IpLocationServiceImpl.java b/novel-front/src/main/java/com/java2nb/novel/service/impl/IpLocationServiceImpl.java index e3c3241..572e9a7 100644 --- a/novel-front/src/main/java/com/java2nb/novel/service/impl/IpLocationServiceImpl.java +++ b/novel-front/src/main/java/com/java2nb/novel/service/impl/IpLocationServiceImpl.java @@ -26,6 +26,7 @@ public class IpLocationServiceImpl implements IpLocationService { try { // 示例返回:"中国|0|湖北省|武汉市|电信" String region = searcher.search(ip); + log.info("IP:{},区域:{}", ip, region); String[] regions = region.split("\\|"); if (regions.length > 0) { // 国家 @@ -38,9 +39,17 @@ public class IpLocationServiceImpl implements IpLocationService { return getLocation(publicIp); } } else if ("中国".equals(country)) { - // 是中国,则返回省份(第三个字段) - String province = regions.length > 2 ? regions[2] : "未知地区"; - // 去掉最后一个“省”字 + // 若为中国,则尝试返回省份(regions[2]) + if (regions.length <= 2) { + // 数据不足,直接返回国家 + return country; + } + String province = regions[2]; + if (!StringUtils.hasText(province) || "0".equals(province)) { + // 省份字段为空或未知,返回国家 + return country; + } + // 去掉末尾的“省”字 return province.endsWith("省") ? province.substring(0, province.length() - 1) : province; } else { // 非中国,返回国家名