获取客户端真实IP
获取本地IP
获取本地host
判断某一IP端口能否连通
package com.app.utils;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
/**
* web常用工具类
*/
public class WebToolUtils
{
/**
* 获取本地IP地址
*/
public static String getLocalIP()
{
if (isWindowsOS())
{
try
{
return InetAddress.getLocalHost().getHostAddress();
}
catch (UnknownHostException e)
{
System.out.println("获取IP地址异常!");
e.printStackTrace();
return null;
}
}
else
{
return getLinuxLocalIp();
}
}
/**
* 判断操作系统是否是Windows
*/
public static boolean isWindowsOS()
{
boolean isWindowsOS = false;
String osName = System.getProperty("os.name");
if (osName.toLowerCase().indexOf("windows") > -1)
{
isWindowsOS = true;
}
return isWindowsOS;
}
/**
* 获取本地Host名称
*/
public static String getLocalHostName()
{
try
{
return InetAddress.getLocalHost().getHostName();
}
catch (UnknownHostException e)
{
e.printStackTrace();
return null;
}
}
/**
* 获取Linux下的IP地址
* @return IP地址
* @throws SocketException
*/
private static String getLinuxLocalIp()
{
String ip = "";
try
{
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
{
NetworkInterface intf = en.nextElement();
String name = intf.getName();
if (!name.contains("docker") && !name.contains("lo"))
{
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
{
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress())
{
String ipaddress = inetAddress.getHostAddress().toString();
if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80"))
{
ip = ipaddress;
}
}
}
}
}
}
catch (SocketException ex)
{
System.out.println("获取IP地址异常!");
ip = null;
ex.printStackTrace();
}
return ip;
}
/**
* 获取用户真实IP地址
*/
public static String getIpAddress(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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getRemoteAddr();
}
return ip;
}
/**
* 判断IP端口是否能够访问到
*/
public static boolean isHostConnectable(String host, int port)
{
Socket socket = new Socket();
try
{
socket.connect(new InetSocketAddress(host, port));
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
finally
{
try
{
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return true;
}
}