java 获取本地的或者外网的IP

源码:public static void getLocalIP(){
   Enumeration<NetworkInterface> netInterfaces = null;
  
   try {
    netInterfaces = NetworkInterface.getNetworkInterfaces();
    while(netInterfaces.hasMoreElements()){
     NetworkInterface ni = netInterfaces.nextElement();
     System.out.println("Display name: "+ni.getDisplayName());
     System.out.println("Name: "+ni.getName());
     System.out.println("MTU: "+ni.getMTU());
    
     Enumeration<InetAddress> ips = ni.getInetAddresses();
     while(ips.hasMoreElements()){
      System.out.println("IP: "+ips.nextElement().getHostAddress());
     }
     System.out.println("..............................");
    }
   } catch (SocketException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
}

详细见:http://www.javaeye.com/topic/169889

posted on 2010-08-18 09:39  画一个圆圈  阅读(187)  评论(0编辑  收藏  举报

导航