九州下载 - 系统安卓苹果手机游戏推荐下载网站!

当前位置:九州下载 > 建站问答 >

java获取linux的ip地址

时间:2022-12-26 17:29编辑:九州下载来源:www.wzjsgs.com

java获取linux的ip地址(java获取linux)"/

java获取linux ip?

代码示例:

/**

* 获取Linux下的IP地址

*

* @return IP地址

* @throws SocketException

*/

public static String getLinuxLocalIp() throws SocketException {

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地址异常");

ex.printStackTrace();

}

System.out.println("IP:" + ip);

return ip;

}

相关文章