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

55 lines
1.5 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;
2018-10-07 14:16:47 +08:00
import com.ruoyi.common.config.Global;
import com.ruoyi.common.json.JSON;
import com.ruoyi.common.json.JSONObject;
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);
public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";
public static String getRealAddressByIP(String ip)
{
2018-10-16 20:21:59 +08:00
String address = "XX XX";
// 内网不查询
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-08-18 20:32:07 +08:00
String rspStr = HttpUtils.sendPost(IP_URL, "ip=" + ip);
if (StringUtils.isEmpty(rspStr))
2018-08-10 14:36:31 +08:00
{
2018-08-18 20:32:07 +08:00
log.error("获取地理位置异常 {}", ip);
return address;
2018-08-10 14:36:31 +08:00
}
2018-10-07 14:16:47 +08:00
JSONObject obj;
try
{
obj = JSON.unmarshal(rspStr, JSONObject.class);
JSONObject data = obj.getObj("data");
String region = data.getStr("region");
String city = data.getStr("city");
address = region + " " + city;
}
catch (Exception e)
{
log.error("获取地理位置异常 {}", ip);
}
2018-07-09 08:44:52 +08:00
}
return address;
}
}