1 package guyu.day0824; 2 3 import java.net.InetAddress; 4 5 /** 6 * @Author: Fred 7 * @Date: 2020/8/24 09:39 8 */ 9 public class Demo01 { 10 public static void main(String[] args) throws Exception { 11 12 //使用 InetAddress 类的 getLocalAddress() 方法获取本机ip地址及主机名 13 InetAddress addr = InetAddress.getLocalHost(); 14 System.out.println("Local HostAddress: "+addr.getHostAddress()); 15 String hostname = addr.getHostName(); 16 System.out.println("Local host name: "+hostname); 17 } 18 }
故屿γ