| | |
| | | |
| | | 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"; |
| | | //https://ip.rss.ink/v1/qqwry?ip=211.147.242.161 |
| | | public static final String IP_URL = "https://ip.rss.ink/v1/qqwry"; |
| | | public static String getRealAddressByIP(String ip) { |
| | | String address = "XX XX"; |
| | | |
| | | if (IpUtils.internalIp(ip)) { |
| | | return "内网IP"; |
| | | } |
| | | if (EssConfig.isAddressEnabled()) { |
| | | String rspStr = HttpUtils.sendPost(IP_URL, "ip=" + ip); |
| | | // if (EssConfig.isAddressEnabled()) { |
| | | // String rspStr = HttpUtils.sendPost(IP_URL, "ip=" + ip); |
| | | // if (StringUtils.isEmpty(rspStr)) { |
| | | // 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; |
| | | // } |
| | | |
| | | if(EssConfig.isAddressEnabled()) |
| | | { |
| | | try { |
| | | String rspStr = HttpUtils.sendGet(IP_URL,"ip=" + ip); |
| | | if (StringUtils.isEmpty(rspStr)) { |
| | | log.error("获取地理位置异常 {}", ip); |
| | | return address; |
| | | } |
| | | JSONObject obj = JSONObject.parseObject(rspStr); |
| | | JSONObject data = obj.getObject("data", JSONObject.class); |
| | | String region = data.getString("region"); |
| | | String province = data.getString("province"); |
| | | String city = data.getString("city"); |
| | | address = region + " " + city; |
| | | String area = data.getString("area"); |
| | | address = province + " " + city+"("+area+")"; |
| | | log.info("ip:"+ip+",地理位置:"+address); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | log.error("获取ip地址地理位置异常:"+ex.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | return address; |
| | | } |
| | | } |