/**
* IP工具类
*/
public class IpUtil
{
private static Logger logger = Logger.getLogger(IpUtil.class);
/**
* 获取登录用户的IP地址
*
* @param request
* @return
*/
public static String getIpAddr(HttpServletRequest request)
{
String ip = request.getHeader("x-forwarded-for");
if ( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) )
{
ip = request.getHeader("Proxy-Client-IP");
}
if ( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) )
{
ip = request.getHeader("WL-Proxy-Client-IP");
}
if ( ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) )
{
ip = request.getRemoteAddr();
}
if ( ip.equals("0:0:0:0:0:0:0:1") )
{
ip = "本地";
}
if ( ip.split(",").length > 1 )
{
ip = ip.split(",")[0];
}
return ip;
}
/**
* 通过IP获取地址
*
* @param ip
* @return
*/
public static String getIpInfo(String ip)
{
String info = "";
try
{
URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
HttpURLConnection htpcon = (HttpURLConnection) url.openConnection();
htpcon.setRequestMethod("GET");
htpcon.setDoOutput(true);
htpcon.setDoInput(true);
htpcon.setUseCaches(false);
InputStream in = htpcon.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
StringBuffer temp = new StringBuffer();
String line = bufferedReader.readLine();
while (line != null)
{
temp.append(line).append("\r\n");
line = bufferedReader.readLine();
}
bufferedReader.close();
return temp.toString();
}
catch (MalformedURLException e)
{
logger.error(e);
}
catch (ProtocolException e)
{
logger.error(e);
}
catch (IOException e)
{
logger.error(e);
}
return info;
}
/**
* 通过IP获取国家
*
* @param ip
* @return
*/
public static String getCountryByIp(String ip)
{
JSONObject obj = (JSONObject) JSON.parse(getIpInfo(ip));
String country = "";
if ( obj != null && obj.getIntValue("code") == 0 )
{
JSONObject data = obj.getJSONObject("data");
country = data.getString("country") + " ";
}
return country.trim();
}
/**
* 通过IP获取省份
*
* @param ip
* @return
*/
public static String getRegionByIp(String ip)
{
JSONObject obj = (JSONObject) JSON.parse(getIpInfo(ip));
String region = "";
if ( obj != null && obj.getIntValue("code") == 0 )
{
JSONObject data = obj.getJSONObject("data");
region = data.getString("region") + " ";
}
return region.trim();
}
/**
* 通过IP获取城市
*
* @param ip
* @return
*/
public static String getCityByIp(String ip)
{
JSONObject obj = (JSONObject) JSON.parse(getIpInfo(ip));
String city = "";
if ( obj != null && obj.getIntValue("code") == 0 )
{
JSONObject data = obj.getJSONObject("data");
city = data.getString("city") + " ";
}
return city.trim();
}
/**
* 通过IP获取国家 地市 行业
*
* @param ip
* @return
*/
public static String getAddressByIp(String ip)
{
JSONObject obj = (JSONObject) JSON.parse(getIpInfo(ip));
StringBuffer address = new StringBuffer();
if ( obj != null && obj.getIntValue("code") == 0 )
{
JSONObject data = obj.getJSONObject("data");
// 国家
String country = data.getString("country");
// 省
String region = data.getString("region");
// 地市
String city = data.getString("city");
if ( StringUtils.isNoneBlank(country) )
{
address.append(country);
address.append(" ");
}
if ( StringUtils.isNoneBlank(region) )
{
address.append(region);
address.append(" ");
}
if ( StringUtils.isNoneBlank(city) )
{
address.append(city);
address.append(" ");
}
}
return address.toString().trim();
}
/**
* @描述: 根据域名获取ip
*/
public static String getIPByDomain(String name)
{
InetAddress address = null;
try
{
address = InetAddress.getByName(name);
}
catch (UnknownHostException e)
{
logger.error(e);
}
return address.getHostAddress().trim().toString();
}
public static void main(String[] args)
{
System.out.println(getIpInfo("117.158.3.54"));
}
}
Java常见帮助类(7)IP查询地址帮助类
最新推荐文章于 2022-11-07 10:42:06 发布