JAVA获取本机IP地址
2016-05-18 21:21
381 查看
一、windows操作系统下主机IP地址
try
{
System.out.println("本机的IP = " + InetAddress.getLocalHost());
} catch (UnknownHostException e)
{
e.printStackTrace();
}
二、linux操作系统下主机IP地址
Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements())
{
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
System.out.println(netInterface.getName());
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements())
{
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address)
{
System.out.println("本机的IP = " + ip.getHostAddress());
}
}
}
try
{
System.out.println("本机的IP = " + InetAddress.getLocalHost());
} catch (UnknownHostException e)
{
e.printStackTrace();
}
二、linux操作系统下主机IP地址
Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements())
{
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
System.out.println(netInterface.getName());
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements())
{
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address)
{
System.out.println("本机的IP = " + ip.getHostAddress());
}
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30316686/viewspace-2102735/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/30316686/viewspace-2102735/
相关文章推荐
- java获取mac地址-屏蔽ip封mac地址
- JAVA 获取当前 内网 和 外网 的IP 地址
- Java程序获取本机IP地址
- java 获取本机ip地址
- linux下java获取本机IP地址
- Linux下java调取命令通过ip获取mac地址
- Java通过IP获取Mac地址:屏蔽Mac地址
- Java获取外网ip地址
- 二、java获取本机ip地址
- Java获取本机外网ip地址的方法
- Java通过ip获取地址
- java获取本地机器所有IP和物理地址
- 利用JAVA接口获取本机IP地址
- 用纯Java代码根据IP获取windows和linux的MAC物理地址
- JAVA获取公网IP地址与内网IP地址方法
- java 获取用户请求的ip 地址
- java获取访问者真实ip和真实地址以及运营商
- java服务器端根据ip获取客户端mac地址
- Java获取访问服务端的客户端IP和MAC地址
- Java 根据IP获取地址