用java如何获得本机IP地址? 不要下面这种方法:这种方法得到的是127.0.0.1。InetAddress addr = InetAddress.getLocalHost();ip=addr.getHostAddress(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 答:1)在你的网卡处理激活状态下,用这种方式当然可以取到本机IP。2)若不论网卡是否处理激活状态,想获取到本机所有的网卡的所有的IP(可能不只有一个网卡),则可用JDK6。0开始的NetworkInterface.getNetworkInterfaces();就可获取到本机 所有的网卡的所有的IP(可能不只有一个网卡)了。如:Enumeration<NetworkInterface> e=NetworkInterface.getNetworkInterfaces(); while(e.hasMoreElements()) { System.out.println(e.nextElement()); }运行结果:(我的本机)name:lo (MS TCP Loopback interface) index: 1 addresses:/127.0.0.1;name:eth0 (VMware Virtual Ethernet Adapter for VMnet8) index: 2 addresses:/192.168.92.1;name:eth1 (VMware Virtual Ethernet Adapter for VMnet1) index: 3 addresses:/192.168.211.1;name:eth2 (Wireless LAN Card - ????ü??????ò?) index: 4 addresses:/192.168.1.199; public static void main(String[] args) throws Exception { InetAddress addr = InetAddress.getLocalHost(); System.out.println(addr.getHostAddress()); }// output 192.168.1.194 try { java.net.InetAddress test = java.net.InetAddress.getByName("localhost") ; System.out.println(test.getLocalHost().getHostAddress());} catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace();} jiangnaisong,牛,应该用它这种方法。 真的是学习到了!回答问题就应该是像jiangnaisong这样,这样才真正学到东西。 ResultSet返回结果精度的问题 多核cpu如何防止死锁 饥渴 java 线程池 问题............................急 有人学过cognos吗? [竞赛]使用JAVA解答华为的笔试题 请高手指导JAVA 的学习方法和购买的书籍 弄了一晚上的Scrollable接口问题,还没有解决,求大侠帮忙!!急 大家用什么工具作java的可视化编程 ? 请教一个问题,jsp域java有什么关系。 SQLServerJDBC配置,帮帮忙!! JNI里的自定义函数如何调用JAVA函数 菜鸟请教了
1)在你的网卡处理激活状态下,用这种方式当然可以取到本机IP。
2)若不论网卡是否处理激活状态,想获取到本机所有的网卡的所有的IP(可能不只有一个网卡),则可用JDK6。0开始的
NetworkInterface.getNetworkInterfaces();就可获取到本机 所有的网卡的所有的IP(可能不只有一个网卡)了。
如:Enumeration<NetworkInterface> e=NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements())
{
System.out.println(e.nextElement());
}运行结果:(我的本机)
name:lo (MS TCP Loopback interface) index: 1 addresses:
/127.0.0.1;name:eth0 (VMware Virtual Ethernet Adapter for VMnet8) index: 2 addresses:
/192.168.92.1;name:eth1 (VMware Virtual Ethernet Adapter for VMnet1) index: 3 addresses:
/192.168.211.1;name:eth2 (Wireless LAN Card - ????ü??????ò?) index: 4 addresses:
/192.168.1.199;
InetAddress addr = InetAddress.getLocalHost();
System.out.println(addr.getHostAddress()); }
// output 192.168.1.194
try {
java.net.InetAddress test = java.net.InetAddress.getByName("localhost") ;
System.out.println(test.getLocalHost().getHostAddress());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}