其实本机获得 一句话就可以搞定
InetAddress.getLocalHost() 但是问题是 这个机子有两个 MAC 两个ip
怎么能获得 一个MAC对应那个IP ????
这个问题
之前我是通过
Runtime.getRuntime().exec("cmd /c ipconfig /all")
但是这样是敲命令阿
还是不行
操作系统不一样就不行了我想只有高手才能解决了 ..
因为我也查询了很多资料 ..
希望高手赐教 ..
InetAddress.getLocalHost() 但是问题是 这个机子有两个 MAC 两个ip
怎么能获得 一个MAC对应那个IP ????
这个问题
之前我是通过
Runtime.getRuntime().exec("cmd /c ipconfig /all")
但是这样是敲命令阿
还是不行
操作系统不一样就不行了我想只有高手才能解决了 ..
因为我也查询了很多资料 ..
希望高手赐教 ..
解决方案 »
- 问 个 解析全国路 问题
- 实现接口..出现了错误
- Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
- 请大家帮忙 简单的布局问题
- Applet的MessageBox问题~~~~急急急急急急~~~
- 请问在java中使用SQL如何处理数据中的"'"问题
- java如何连接数据库?
- 关于outofmemory的问题,请高手赐教
- 请问,在jdk1-2-2中,import所需的东西在什么地方,我怎么找不到阿
- 急外包JAVA项目
- 请教各位大侠,如何根据JTabel 的内容来决定他的列是否可编辑?
- 关于eclipse开发的困惑
public String getLocalHostName() {
String hostName;
try {
InetAddress addr = InetAddress.getLocalHost();
hostName = addr.getHostName();
} catch (Exception ex) {
hostName = "";
}
return hostName;
}public String[] getAllLocalHostIP() {
String[] ret = null;
try {
String hostName = getLocalHostName();
if (hostName.length() > 0) {
InetAddress[] addrs = InetAddress.getAllByName(hostName);
if (addrs.length > 0) {
ret = new String[addrs.length];
for (int i = 0; i < addrs.length; i++) {
ret[i] = addrs[i].getHostAddress();
}
}
} } catch (Exception ex) {
ret = null;
}
return ret;
}
我测试了!是可以的!我的是VMware Network Adapter VMnet1,VMware Network Adapter VMnet8,LAN
192.168.1.104
192.168.6.1
192.168.80.1网线断开是不能测试到的
* @(#)StringUtil .java
* 版权声明 YaYiSoft 版权所有 违者必究
*
* 修订记录:
* 1)更改者:Easydozer
* 时 间:2008-10-11
* 描 述:创建
*/import java.net.InetAddress;/**
*
*
*
*
* @author Mead Lai
* @version 1.0
* @see
* @since 1.0
*/
class StringUtil {
public String getLocalHostName() {
String hostName;
try {
InetAddress addr = InetAddress.getLocalHost();
hostName = addr.getHostName();
} catch (Exception ex) {
hostName = "";
}
return hostName;
} public String[] getAllLocalHostIP() {
String[] ret = null;
try {
String hostName = getLocalHostName();
if (hostName.length() > 0) {
InetAddress[] addrs = InetAddress.getAllByName(hostName);
if (addrs.length > 0) {
ret = new String[addrs.length];
for (int i = 0; i < addrs.length; i++) {
ret[i] = addrs[i].getHostAddress();
}
}
} } catch (Exception ex) {
ret = null;
}
return ret;
} public static void main(String[] args) { StringUtil su = new StringUtil();
String[] ip = su.getAllLocalHostIP();
for (String s : ip) {
System.out.println(s);
}
}}
解决了
十分感谢你 ..
但是还有一些别的问题 ..
我如果想拿java 修改 ip
我不想用 Runtime.getRuntime().exec()
这种命令的方式修改
我还可以用什么方法呢?
正在考虑中 ..
你有什么好的办法吗?