import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
/*获取IP地址*/
public class CatchIp
{
private InetAddress LocalIP = null;
private InetAddress ServerIP = null;
public static void main(String[] args)
{
CatchIp mytest;
mytest = new CatchIp();
System.out.println("LocalHost IP is:"+mytest.catchLocalIP());
System.out.println("Server IP is:"+mytest.catchServerIP());
}
//取得本机IP地址
public InetAddress catchLocalIP()
{
try
{
LocalIP = InetAddress.getLocalHost();
} catch (UnknownHostException e)
{
}
return LocalIP;
}
//取得服务器网络地址
public InetAddress catchServerIP()
{
try {
ServerIP = InetAddress.getByName("www.sina.com.cn");
//取得www.sina.com.cn的IP地址
} catch (UnknownHostException e)
{
System.out.println(e.getMessage());
}
return ServerIP;
}
}
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">在网上找了几个用</span><a target=_blank href="http://lib.csdn.net/base/17" class="replace_word" title="Java EE知识库" target="_blank" style="color: rgb(223, 52, 52); text-decoration: none; font-family: Arial; font-size: 14px; line-height: 26px; font-weight: bold;">Java</a><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">获取本机IP地址的代码,发现都少都有些不完美,自己整理了一下.突然之间很想把自己的IP地址给获取了,虽然用系统自带命令可以得到,但自己想写一个程序获取一下,到网上搜索了一下java获取本机IP地址的方法,结果居然发现没有一个是可以用的,气的我老人家吐血,</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">这些人闭着眼睛写程序,写完了就往网上发,也不测试一下,害的我以为自己RP问题,老是获取不到正确的IP地址,强烈谴责!!!</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">为了表示鄙视,现把网上找到的主要的两种方法的不足给指出一下</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">方法一(只能在Windows上使用,Linux平台就gei屁了):</span><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" />
ry
{
System.out.println("本机的IP = " + InetAddress.getLocalHost());
} catch (UnknownHostException e)
{
e.printStackTrace();
}
在Linux下的执行结果是:本机的IP = xxx/127.0.1.1 (其中xxx是你的计算机名,偶这里马赛克了)
方法二(宣称可以在Linux下执行)
Enumeration netInterfaces=NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while(netInterfaces.hasMoreElements())
{
NetworkInterface ni=(NetworkInterface)netInterfaces.nextElement();
System.out.println(ni.getName());
ip=(InetAddress) ni.getInetAddresses().nextElement();
if( !ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":")==-1)
{
System.out.println ("本机的ip=" + ip.getHostAddress());
break;
}
else
{
ip=null;
}
}
从红色部分的代码可以看到,该代码对于获取到的第一个NetworkInterface的IP地址的获取,没有循环的获取,只是对第一个IP地址进行了处理,这样就导致了如果第一个IP地址不是一个
Inet4Address的地址而是一个< span
id="ArticleContent1_ArticleContent1_lblContent">Inet6Address,这个判断 ip.getHostAddress().indexOf(":")==-1将永远是false,这个if条件进不去呀,多害人,强烈鄙视!
不过方法二思路是对了,就是有些小毛病,让偶修改了一下,最终版的可以在 Linux下正确执行的代码如下:
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());
}
}
}