代码之家  ›  专栏  ›  技术社区  ›  giri

如何从Java中获取域名的IP地址?

  •  19
  • giri  · 技术社区  · 16 年前

    我正在编写一个需要IP地址的应用程序。我有一个域名,我想知道如何从中获取IP地址。例如,“www.girionjava.com”。如何用Java编程获取这个网站的IP地址?谢谢。

    4 回复  |  直到 11 年前
        1
  •  28
  •   Powerlord    16 年前
    InetAddress giriAddress = java.net.InetAddress.getByName("www.girionjava.com");
    

    然后,如果您希望IP作为字符串

    String address = giriAddress.getHostAddress();
    
        2
  •  6
  •   Joachim Sauer    16 年前
    InetAddress.getByName("www.girionjava.com")
    
        3
  •  6
  •   redoc    13 年前

    这应该很简单。

    InetAddress[] machines = InetAddress.getAllByName("yahoo.com");
    for(InetAddress address : machines){
      System.out.println(address.getHostAddress());
    }
    
        4
  •  0
  •   M. Jessup    16 年前

    (额外的掩码在打印Java中考虑所有要签名的整数,但是IP地址是无符号的)

    InetAddress[] machines = InetAddress.getAllByName("yahoo.com");
    for(InetAddress address : machines){
      byte[] ip = address.getAddress();
      for(byte b : ip){
        System.out.print(Integer.toString(((int)b)&0xFF)+".");
      }
      System.out.println();
    }
    
    推荐文章