yyt
8 天以前 0cceb649e1dc443c2a91d26d81eacb0867c96db3
提交 | 用户 | 时间
0cceb6 1 package com.jcdm.common.utils.ip;
Y 2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import com.alibaba.fastjson2.JSON;
6 import com.alibaba.fastjson2.JSONObject;
7 import com.jcdm.common.config.MesConfig;
8 import com.jcdm.common.constant.Constants;
9 import com.jcdm.common.utils.StringUtils;
10 import com.jcdm.common.utils.http.HttpUtils;
11
12 /**
13  * 获取地址类
14  * 
15  * @author jc
16  */
17 public class AddressUtils
18 {
19     private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
20
21     // IP地址查询
22     public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
23
24     // 未知地址
25     public static final String UNKNOWN = "XX XX";
26
27     public static String getRealAddressByIP(String ip)
28     {
29         // 内网不查询
30         if (IpUtils.internalIp(ip))
31         {
32             return "内网IP";
33         }
34         if (MesConfig.isAddressEnabled())
35         {
36             try
37             {
38                 String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
39                 if (StringUtils.isEmpty(rspStr))
40                 {
41                     log.error("获取地理位置异常 {}", ip);
42                     return UNKNOWN;
43                 }
44                 JSONObject obj = JSON.parseObject(rspStr);
45                 String region = obj.getString("pro");
46                 String city = obj.getString("city");
47                 return String.format("%s %s", region, city);
48             }
49             catch (Exception e)
50             {
51                 log.error("获取地理位置异常 {}", ip);
52             }
53         }
54         return UNKNOWN;
55     }
56 }