注意:此方法只有在jdk5以上才支持。。。
public
static
void
main(String[] args)
...
{ try ...{ Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; while (netInterfaces.hasMoreElements()) ...{ NetworkInterface ni = (NetworkInterface) netInterfaces .nextElement(); // System.out.println(ni.getName()); ip = (InetAddress) ni.getInetAddresses().nextElement(); System.out.println("IP:" + ip.getHostAddress()); System.out.println(ip.isSiteLocalAddress()); System.out.println(ip.isLoopbackAddress()); if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) ...{ System.out.println("本机的ip=" + ip.getHostAddress()); break; } else ...{ ip = null; } } } catch (SocketException e) ...{ e.printStackTrace(); } }