Files
RuoYi/ruoyi-common/src/main/java/com/ruoyi/common/utils/AddressUtils.java

56 lines
1.6 KiB
Java
Raw Normal View History

2018-07-09 08:44:52 +08:00
package com.ruoyi.common.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2020-05-13 21:16:15 +08:00
import com.alibaba.fastjson.JSONObject;
2018-10-07 14:16:47 +08:00
import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
2018-10-07 14:16:47 +08:00
import com.ruoyi.common.utils.http.HttpUtils;
2018-07-09 08:44:52 +08:00
/**
* 获取地址类
*
* @author ruoyi
*/
public class AddressUtils
{
private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
2020-05-13 21:16:15 +08:00
// IP地址查询
public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
// 未知地址
public static final String UNKNOWN = "XX XX";
2018-07-09 08:44:52 +08:00
public static String getRealAddressByIP(String ip)
{
2020-05-13 21:16:15 +08:00
String address = UNKNOWN;
2018-10-16 20:21:59 +08:00
// 内网不查询
if (IpUtils.internalIp(ip))
{
return "内网IP";
}
2018-10-07 14:16:47 +08:00
if (Global.isAddressEnabled())
2018-07-09 08:44:52 +08:00
{
2018-10-07 14:16:47 +08:00
try
{
String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
2020-05-13 21:16:15 +08:00
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);
2018-10-07 14:16:47 +08:00
}
catch (Exception e)
{
log.error("获取地理位置异常 {}", e);
2018-10-07 14:16:47 +08:00
}
2018-07-09 08:44:52 +08:00
}
return address;
}
}