Java获取本机的IP和主机名

//获取本机的IP地址
public String getLocalHostIp()throws Exception{
		InetAddress addr = InetAddress.getLocalHost();  
        String ip=addr.getHostAddress().toString();
        return ip;
}
//获取本机的主机名
public String getLocalHostName()throws Exception{
		InetAddress addr = InetAddress.getLocalHost();  
        String name=addr.getHostName().toString();
        return name;
}

你可能感兴趣的:(Java获取本机的IP和主机名)