update 优化 使用try-with-resource自动关闭AddressUtils类中的Http响应流以免出现内存泄漏

Signed-off-by: 枕梦记 <545073804@qq.com>
This commit is contained in:
枕梦记 2022-09-21 04:02:31 +00:00 committed by Gitee
parent 2c39f26de3
commit 1d70b8096d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -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);