-
- Windows下:获取机制
- InetAddress inet = InetAddress.getLocalHost();
- System.out.println("本机的ip=" + inet.getHostAddress());
- Linux下:获取机制
- //根据网卡取本机配置的IP
- 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();
- if( !ip.isSiteLocalAddress()
- && !ip.isLoopbackAddress()
- && ip.getHostAddress().indexOf(":")==-1)
- {
- System.out.println("本机的ip=" + ip.getHostAddress());
- break;
- }
- else
- {
- ip=null;
- }
- }