Java获取本机IP地址

时间:2025-04-03 09:40:34
import java.net.InetAddress; import java.net.UnknownHostException; public class Test { public static void main(String[] args) { try{ InetAddress localHost = InetAddress.getLocalHost(); System.out.println("主机名:"+localHost.getHostName()); System.out.println("本地ip地址:"+localHost.getHostAddress()); } catch(UnknownHostException e) { e.printStackTrace(); } } }