以下是使用Java打印出域名的示例代码:

import java.net.*;

public class DomainNamePrinter {
    public static void main(String[] args) {
        try {
            InetAddress address = InetAddress.getLocalHost();
            String domainName = address.getCanonicalHostName();
            System.out.println("Domain name: " + domainName);
        } catch (UnknownHostException ex) {
            System.out.println("Could not determine domain name: " + ex.getMessage());
        }
    }
}

此代码使用InetAddress类获取本地主机的IP地址,然后使用getCanonicalHostName()方法获取其规范域名。如果出现任何错误,则会捕获UnknownHostException并打印错误消息

java打印出域名

原文地址: https://www.cveoy.top/t/topic/hrTY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录