您的位置:首页 > 运维架构 > Linux

linux/Windows中JAVA获取IP地址

2012-03-21 19:19 323 查看
public  void getIpAddr() {
Enumeration allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {

e.printStackTrace();
}
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
.nextElement();
System.out.println(netInterface.getName());
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address
&& ip.getHostAddress().indexOf(".") != -1) {
System.out.println("本机的IP = " + ip.getHostAddress());
logger.debug("------------------本机的IP : "+ip.getHostAddress()+"---------------------");
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: