获取本机ip地址以及主机名称


package guyu.day0824;

import java.net.InetAddress;

/**
* @Author: LHT
* @Date: 2020/8/24 09:39
*/
public class Demo01 {
public static void main(String[] args) throws Exception {

//使用 InetAddress 类的 getLocalAddress() 方法获取本机ip地址及主机名
InetAddress addr = InetAddress.getLocalHost();
System.out.println("Local HostAddress: "+addr.getHostAddress());
String hostname = addr.getHostName();
System.out.println("Local host name: "+hostname);
}
}

原文地址:https://www.cnblogs.com/liuhaotian548/p/13552267.html