Java程序获取本地真实ip地址---window/linux/docker环境
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static String getLocalIp() { String ip=""; if (System.getProperty("os.name").toLowerCase().indexOf("windows")>-1) { try { ip= InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { logger.error("UnknownHostException {}",e); } }else { try { for(Enumeration<NetworkInterface> en= NetworkInterface.getNetworkInterfaces();en.hasMoreElements();) { NetworkInterface interf = en.nextElement(); String name= interf.getName(); if (!name.contains("docker")&&!name.contains("lo")) { for(Enumeration<InetAddress>enumeAddress=interf.getInetAddresses();enumeAddress.hasMoreElements();) { InetAddress address= enumeAddress.nextElement(); if (!address.isLoopbackAddress()) { String ipAddress= address.getHostAddress().toString(); if (!ipAddress.contains("::")&&!ipAddress.contains("0:0")&&!ipAddress.contains("fe80")) { ip= ipAddress; } } } } } } catch (Exception e) { logger.error("get Linux local ip error {}",e); } } return ip; }