源代码:
package test;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class HostInfoTest {
public static void main(String[] args) {
System.out.println(new HostInfoTest().getHostInfo());
}
/**
* 获取本地IPd等信息
* @return
*/
private String getHostInfo(){
StringBuffer hostInfo = new StringBuffer();
InetAddress address;
try {
address = InetAddress.getLocalHost();
hostInfo.append("本机IP:\n");
hostInfo.append(address.getHostName());
hostInfo.append("\nIP地址\n");
hostInfo.append(address.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
return hostInfo.toString();
}
}
控制台输出结果:
本机IP:
meteor-PC
IP地址
169.254.94.108
如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。