From 1d70b8096da81883caea172a63e27610693701cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=95=E6=A2=A6=E8=AE=B0?= <545073804@qq.com> Date: Wed, 21 Sep 2022 04:02:31 +0000 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8try-with-resource=E8=87=AA=E5=8A=A8=E5=85=B3=E9=97=ADA?= =?UTF-8?q?ddressUtils=E7=B1=BB=E4=B8=AD=E7=9A=84Http=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E6=B5=81=E4=BB=A5=E5=85=8D=E5=87=BA=E7=8E=B0=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 枕梦记 <545073804@qq.com> --- .../ruoyi/common/utils/ip/AddressUtils.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) 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);