Commit f3d0bf6c2697d8a79059bbba5eb9af7e92d7208d
1 parent
ddcabaa6
更换IP地址查询接口
Showing
2 changed files
with
21 additions
and
11 deletions
ruoyi/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java
| @@ -49,7 +49,7 @@ public class HttpUtils | @@ -49,7 +49,7 @@ public class HttpUtils | ||
| 49 | connection.setRequestProperty("connection", "Keep-Alive"); | 49 | connection.setRequestProperty("connection", "Keep-Alive"); |
| 50 | connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); | 50 | connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
| 51 | connection.connect(); | 51 | connection.connect(); |
| 52 | - in = new BufferedReader(new InputStreamReader(connection.getInputStream())); | 52 | + in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK")); |
| 53 | String line; | 53 | String line; |
| 54 | while ((line = in.readLine()) != null) | 54 | while ((line = in.readLine()) != null) |
| 55 | { | 55 | { |
ruoyi/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java
| @@ -16,11 +16,15 @@ public class AddressUtils | @@ -16,11 +16,15 @@ public class AddressUtils | ||
| 16 | { | 16 | { |
| 17 | private static final Logger log = LoggerFactory.getLogger(AddressUtils.class); | 17 | private static final Logger log = LoggerFactory.getLogger(AddressUtils.class); |
| 18 | 18 | ||
| 19 | - public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php"; | 19 | + // IP地址查询 |
| 20 | + public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp"; | ||
| 21 | + | ||
| 22 | + // 未知地址 | ||
| 23 | + public static final String UNKNOWN = "XX XX"; | ||
| 20 | 24 | ||
| 21 | public static String getRealAddressByIP(String ip) | 25 | public static String getRealAddressByIP(String ip) |
| 22 | { | 26 | { |
| 23 | - String address = "XX XX"; | 27 | + String address = UNKNOWN; |
| 24 | // 内网不查询 | 28 | // 内网不查询 |
| 25 | if (IpUtils.internalIp(ip)) | 29 | if (IpUtils.internalIp(ip)) |
| 26 | { | 30 | { |
| @@ -28,17 +32,23 @@ public class AddressUtils | @@ -28,17 +32,23 @@ public class AddressUtils | ||
| 28 | } | 32 | } |
| 29 | if (RuoYiConfig.isAddressEnabled()) | 33 | if (RuoYiConfig.isAddressEnabled()) |
| 30 | { | 34 | { |
| 31 | - String rspStr = HttpUtils.sendPost(IP_URL, "ip=" + ip); | ||
| 32 | - if (StringUtils.isEmpty(rspStr)) | 35 | + try |
| 36 | + { | ||
| 37 | + String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true"); | ||
| 38 | + if (StringUtils.isEmpty(rspStr)) | ||
| 39 | + { | ||
| 40 | + log.error("获取地理位置异常 {}", ip); | ||
| 41 | + return UNKNOWN; | ||
| 42 | + } | ||
| 43 | + JSONObject obj = JSONObject.parseObject(rspStr); | ||
| 44 | + String region = obj.getString("pro"); | ||
| 45 | + String city = obj.getString("city"); | ||
| 46 | + return String.format("%s %s", region, city); | ||
| 47 | + } | ||
| 48 | + catch (Exception e) | ||
| 33 | { | 49 | { |
| 34 | log.error("获取地理位置异常 {}", ip); | 50 | log.error("获取地理位置异常 {}", ip); |
| 35 | - return address; | ||
| 36 | } | 51 | } |
| 37 | - JSONObject obj = JSONObject.parseObject(rspStr); | ||
| 38 | - JSONObject data = obj.getObject("data", JSONObject.class); | ||
| 39 | - String region = data.getString("region"); | ||
| 40 | - String city = data.getString("city"); | ||
| 41 | - address = region + " " + city; | ||
| 42 | } | 52 | } |
| 43 | return address; | 53 | return address; |
| 44 | } | 54 | } |