请问如何能显示本机的IP?
------解决方案--------------------
运行--> cmd--> ipconfig
------解决方案--------------------
开始—〉运行-〉cmd-〉ipconfig-> 回车
IP Address这一行就是你的IP地址
------解决方案--------------------
//根据网卡取本机配置的IP
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;
}
}
如果只是想找到本地IP. 2楼的正解!
------解决方案--------------------
import java.net.InetAddress;
public class GetLocalIp {
public static String getLocalHostIP(){
String ret = " ";
try{
InetAddress addr = InetAddress.getLocalHost();
String hostName = addr.getHostName();
if(hostName.length()> 0){
InetAddress[] addrs = InetAddress.getAllByName(hostName);
ret = addrs[1].getHostAddress();
}
}
catch(Exception ex){
ret = null;
}
return ret;
}
public static void main(String[] args) {
System.out.println( getLocalHostIP());
}
}
------解决方案--------------------
ret = addrs[1].getHostAddress();
这句好像要改成
ret = addrs[0].getHostAddress();
------解决方案--------------------
六楼的兄弟是正解,不过ret = addrs[1].getHostAddress();输出网络地址,不是本机自己设置的地址,ret = addrs[0].getHostAddress();是输出本机的地址,ex :192.168.X.X:-)