返回顶部

收藏

java实现根据域名获得ip地址代码

更多
mport java.net.InetAddress;
import java.net.UnknownHostException;

public class GetIPAddressFromHostname {

    public static void main(String[] args) {

        try {

            InetAddress inetAddr = InetAddress.getByName("open-open.com");

            byte[] addr = inetAddr.getAddress();

            // Convert to dot representation
            String ipAddr = "";
            for (int i = 0; i < addr.length; i++) {
                if (i > 0) {
                    ipAddr += ".";
                }
                ipAddr += addr[i] &amp; 0xFF;
            }

            System.out.println("IP Address: " + ipAddr);

        }
        catch (UnknownHostException e) {
            System.out.println("Host not found: " + e.getMessage());
        }

    }

}

标签:java

收藏

0人收藏

支持

0

反对

0

发表评论