以下实例演示了如何使用 InetAddress 类的 getLocalAddress() 方法获取本机ip地址及主机名:
import java.net.InetAddress;
public class Main {
public static void main(String[] args)
throws Exception {
InetAddress addr = InetAddress.getLocalHost();
System.out.println("Local HostAddress:
"+addr.getHostAddress());
String hostname = addr.getHostName();
System.out.println("Local host name: "+hostname);
}
}

本文通过一个Java示例程序介绍了如何使用InetAddress类的getLocalHost()方法来获取本地计算机的IP地址和主机名。
561

被折叠的 条评论
为什么被折叠?



