diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java index 8d3515e7f..5e2c2f2ed 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java @@ -3,6 +3,7 @@ package com.ruoyi.common.utils.ip; import cn.hutool.core.lang.Dict; import cn.hutool.core.net.NetUtil; import cn.hutool.http.HtmlUtil; +import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; import com.ruoyi.common.config.RuoYiConfig; import com.ruoyi.common.constant.Constants; @@ -28,9 +29,8 @@ public class AddressUtils { public static final String UNKNOWN = "XX XX"; public static String getRealAddressByIP(String ip) { - String address = UNKNOWN; if (StringUtils.isBlank(ip)) { - return address; + return UNKNOWN; } // 内网不查询 ip = "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : HtmlUtil.cleanHtmlTag(ip); @@ -38,18 +38,15 @@ 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)) { + try (HttpResponse response = HttpUtil.createGet(IP_URL).body("ip=" + ip + "&json=true", Constants.GBK).execute()) { + String responseBody = response.body(); + if (StringUtils.isBlank(responseBody)) { log.error("获取地理位置异常 {}", ip); return UNKNOWN; } - Dict obj = JsonUtils.parseMap(rspStr); - String region = obj.getStr("pro"); - String city = obj.getStr("city"); + Dict dict = JsonUtils.parseMap(responseBody); + String region = dict.getStr("pro"); + String city = dict.getStr("city"); return String.format("%s %s", region, city); } catch (Exception e) { log.error("获取地理位置异常 {}", ip);