不要下面这种方法:这种方法得到的是127.0.0.1。
InetAddress addr = InetAddress.getLocalHost();
ip=addr.getHostAddress();

解决方案 »

  1.   

    答:
    1)在你的网卡处理激活状态下,用这种方式当然可以取到本机IP。
    2)若不论网卡是否处理激活状态,想获取到本机所有的网卡的所有的IP(可能不只有一个网卡),则可用JDK6。0开始的
    NetworkInterface.getNetworkInterfaces();就可获取到本机 所有的网卡的所有的IP(可能不只有一个网卡)了。
    如:Enumeration<NetworkInterface> e=NetworkInterface.getNetworkInterfaces();
         while(e.hasMoreElements())
         {
         System.out.println(e.nextElement());
         }运行结果:(我的本机)
    name:lo (MS TCP Loopback interface) index: 1 addresses:
    /127.0.0.1;name:eth0 (VMware Virtual Ethernet Adapter for VMnet8) index: 2 addresses:
    /192.168.92.1;name:eth1 (VMware Virtual Ethernet Adapter for VMnet1) index: 3 addresses:
    /192.168.211.1;name:eth2 (Wireless LAN Card - ????ü??????ò?) index: 4 addresses:
    /192.168.1.199;
      

  2.   

    public static void main(String[] args) throws Exception {
    InetAddress addr = InetAddress.getLocalHost(); 
    System.out.println(addr.getHostAddress()); }
    // output 192.168.1.194
      

  3.   


    try {
    java.net.InetAddress   test   =   java.net.InetAddress.getByName("localhost")   ;
    System.out.println(test.getLocalHost().getHostAddress());
    } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
            e.printStackTrace();
    }
      

  4.   

        jiangnaisong,牛,应该用它这种方法。
      

  5.   

    真的是学习到了!回答问题就应该是像jiangnaisong这样,这样才真正学到东西。