InetAddress类此类表示Internet协议(IP)地址。您可以获取本地IP地址和 使用此类的getLocalAddress() 方法的系统的主机名
import java.net.InetAddress; public class GetIpAddress { 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); } }
输出结果
Local HostAddress: 192.168.25.1 Local host name: Nhooo