cn.t.util.common.IpUtil Maven / Gradle / Ivy
package cn.t.util.common;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class IpUtil {
/**
* 获取本地ip
* @return xxx
*/
public static String getLocalIp() {
StringBuilder ip = new StringBuilder();
try {
for (Enumeration en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface ni = en.nextElement();
for (Enumeration enumIpAdd = ni
.getInetAddresses(); enumIpAdd.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAdd.nextElement();
if (!inetAddress.isLoopbackAddress()
&& !inetAddress.isLinkLocalAddress()
&& inetAddress.isSiteLocalAddress()) {
ip.append(inetAddress.getHostAddress());
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return ip.toString();
}
}