From f3d0bf6c2697d8a79059bbba5eb9af7e92d7208d Mon Sep 17 00:00:00 2001 From: RuoYi Date: Wed, 13 May 2020 21:11:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=8D=A2IP=E5=9C=B0=E5=9D=80=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/utils/http/HttpUtils.java | 2 +- .../ruoyi/common/utils/ip/AddressUtils.java | 30 ++++++++++++------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/ruoyi/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java b/ruoyi/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java index 323dbc1..4700436 100644 --- a/ruoyi/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java +++ b/ruoyi/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java @@ -49,7 +49,7 @@ public class HttpUtils connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.connect(); - in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK")); String line; while ((line = in.readLine()) != null) { diff --git a/ruoyi/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java b/ruoyi/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java index 2709f45..8402a0d 100644 --- a/ruoyi/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java +++ b/ruoyi/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java @@ -16,11 +16,15 @@ public class AddressUtils { private static final Logger log = LoggerFactory.getLogger(AddressUtils.class); - public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php"; + // IP地址查询 + public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp"; + + // 未知地址 + public static final String UNKNOWN = "XX XX"; public static String getRealAddressByIP(String ip) { - String address = "XX XX"; + String address = UNKNOWN; // 内网不查询 if (IpUtils.internalIp(ip)) { @@ -28,17 +32,23 @@ public class AddressUtils } if (RuoYiConfig.isAddressEnabled()) { - String rspStr = HttpUtils.sendPost(IP_URL, "ip=" + ip); - if (StringUtils.isEmpty(rspStr)) + try + { + String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true"); + if (StringUtils.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; } - JSONObject obj = JSONObject.parseObject(rspStr); - JSONObject data = obj.getObject("data", JSONObject.class); - String region = data.getString("region"); - String city = data.getString("city"); - address = region + " " + city; } return address; }