Java获取本机ip地址及主机名
package xuexi.Java.Two;
import java.net.InetAddress;
//演示如何使用 InetAddress 类的 getLocalAddress() 方法获取本机ip地址及主机名
public class Main2 {
public static void main(String[] args)
throws Exception {
InetAddress addr = InetAddress.getLocalHost();
System.out.println("主机地址:"+addr.getHostAddress());
String hostname = addr.getHostName();
System.out.println("主机名称: "+hostname);
}
}