其实本机获得 一句话就可以搞定

InetAddress.getLocalHost() 
但是问题是 这个机子有两个 MAC 两个ip
怎么能获得 一个MAC对应那个IP ????
这个问题 
之前我是通过
Runtime.getRuntime().exec("cmd /c ipconfig /all")
但是这样是敲命令阿
还是不行 
操作系统不一样就不行了我想只有高手才能解决了 ..
因为我也查询了很多资料 ..
希望高手赐教 ..
 

解决方案 »

  1.   


    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;
        }
      

  2.   


    我测试了!是可以的!我的是VMware Network Adapter VMnet1,VMware Network Adapter VMnet8,LAN
    192.168.1.104
    192.168.6.1
    192.168.80.1网线断开是不能测试到的

      

  3.   

    完整的测试代码/**
     * @(#)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);
    }
    }}
      

  4.   



    解决了
    十分感谢你 .. 
    但是还有一些别的问题 ..
    我如果想拿java 修改 ip 
    我不想用 Runtime.getRuntime().exec()
    这种命令的方式修改
    我还可以用什么方法呢?
    正在考虑中 ..
    你有什么好的办法吗?