java.InetAddress.getLocalHost()不是一种通用的功能,由于这个是得到系统网络配置文件的内容,例如*nix下面就是/etc/hosts的内容。如果要得到一种和OS有关的网卡配置内容,就要运用java.NetworkInterface.getNetworkInterface()才干取得和java语言完成相关的localhost信息。
import java.*;public class kg { public static void main(String args[]){ try{ java.InetAddress ad=java.InetAddress.getLocalHost(); System.out.println(ad.getAddress()); System.out.println(ad.getCanonicalHostName()); System.out.println(ad.getHostName()); System.out.println(ad.isMulticastAddress()); System.out.println(ad.isLoopbackAddress()); /*Windows output * [B@757aef * localhost * cpit-b524f2dfb2 * false * true */ java.util.Enumerationjava.NetworkInterface en=java.NetworkInterface.getNetworkInterfaces(); while(en.hasMoreElements()){ java.NetworkInterface ni=en.nextElement(); System.
smartyou钱包怎么样out.println("x:"+ni.getName()); System.out.println("y:"+ni.getDisplayName()); java.util.Enumerationjava.InetAddress ads=ni.getInetAddresses(); while(ads.hasMoreElements()){ java.InetAddress ip=ads.nextElement(); if(!ip.isSiteLocalAddress() !ip.isLoopbackAddress() !(ip.getHostAddress().indexOf(":")==-1)){ System.out.println("_My ip is:"+ip.getHostAddress()); }//end if } } /* Windows output * x:lo * y:MS TCP Loopback interface * x:eth0 * y:Broadcom 440x 10/100 Integrated Controller */ }catch(java.UnknownHostException e){ /* Linux out all: * [B@19821f * localhost.localdomain * localhost.localdomain * false * true * x:lo * y:lo */ }catch(java.SocketException e){ System.out.println("Socket exception"); } }}