From b3541e975898e17a6cbc1a9ea776f3ec7e91d39a Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期六, 12 六月 2021 20:13:35 +0800 Subject: [PATCH] update 优化 使用 hutool 替换 ruoyi 自带工具类 解决部分方法过期与高版本JDK不兼容问题 --- ruoyi-common/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java | 77 ++++++++++++++++++-------------------- 1 files changed, 36 insertions(+), 41 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 4805d25..2093e19 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 @@ -1,56 +1,51 @@ package com.ruoyi.common.utils.ip; +import cn.hutool.core.net.NetUtil; import cn.hutool.core.util.StrUtil; +import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSONObject; import com.ruoyi.common.config.RuoYiConfig; import com.ruoyi.common.constant.Constants; -import com.ruoyi.common.utils.http.HttpUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; /** * 鑾峰彇鍦板潃绫� - * + * * @author ruoyi */ -public class AddressUtils -{ - private static final Logger log = LoggerFactory.getLogger(AddressUtils.class); +@Slf4j +public class AddressUtils { - // IP鍦板潃鏌ヨ - public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp"; + // IP鍦板潃鏌ヨ + public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp"; - // 鏈煡鍦板潃 - public static final String UNKNOWN = "XX XX"; + // 鏈煡鍦板潃 + public static final String UNKNOWN = "XX XX"; - public static String getRealAddressByIP(String ip) - { - String address = UNKNOWN; - // 鍐呯綉涓嶆煡璇� - if (IpUtils.internalIp(ip)) - { - return "鍐呯綉IP"; - } - if (RuoYiConfig.isAddressEnabled()) - { - try - { - String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK); - if (StrUtil.isEmpty(rspStr)) - { - log.error("鑾峰彇鍦扮悊浣嶇疆寮傚父 {}", ip); - return UNKNOWN; - } - JSONObject obj = JSONObject.parseObject(rspStr); - String region = obj.getString("pro"); - String city = obj.getString("city"); - return String.format("%s %s", region, city); - } - catch (Exception e) - { - log.error("鑾峰彇鍦扮悊浣嶇疆寮傚父 {}", ip); - } - } - return address; - } + public static String getRealAddressByIP(String ip) { + String address = UNKNOWN; + // 鍐呯綉涓嶆煡璇� + if (NetUtil.isInnerIP(ip)) { + return "鍐呯綉IP"; + } + if (RuoYiConfig.isAddressEnabled()) { + try { + String rspStr = HttpUtil.createGet(IP_URL) + .body("ip=" + ip + "&json=true", Constants.GBK) + .execute() + .body(); + if (StrUtil.isEmpty(rspStr)) { + log.error("鑾峰彇鍦扮悊浣嶇疆寮傚父 {}", ip); + return UNKNOWN; + } + JSONObject obj = JSONObject.parseObject(rspStr); + String region = obj.getString("pro"); + String city = obj.getString("city"); + return String.format("%s %s", region, city); + } catch (Exception e) { + log.error("鑾峰彇鍦扮悊浣嶇疆寮傚父 {}", ip); + } + } + return address; + } } -- Gitblit v1.9.3