http://www.cn-java.com/target/news.php?news_id=1053

解决方案 »

  1.   

    可以在java调用 ipconfig/all命令import java.lang.*;
    import java.io.*;
    import java.util.*;
    public class Util_syscmd{ /**
     * @param shellCommand
     * 
     */
    public Vector execute(String shellCommand){
    try{
    Start(shellCommand);
    Vector vResult=new Vector();
    DataInputStream in=new DataInputStream(p.getInputStream());
    BufferedReader reader=new BufferedReader(new InputStreamReader(in)); String line;
    do{
    line=reader.readLine();
    if (line==null){
    break;
    }
    else{
    vResult.addElement(line);
    }
    }while(true);
    reader.close();
    return vResult; }catch(Exception e){
    //error
    return null;
    }
    }
    /**
     * @param shellCommand
     * 
     */
    public void Start(String shellCommand){
    try{
    if(p!=null){
    kill();
    }
    Runtime sys= Runtime.getRuntime();
    p=sys.exec(shellCommand);
    }catch(Exception e){
    System.out.println(e.toString());
    }
    }
    /**
    kill this process
    */
    public void kill(){
    if(p!=null){
    p.destroy();
    p=null;
    }
    }

    Process p;
    }
      

  2.   

    然后设计一个GetPhysicalAddress类,这个类用来调用Util_syscmd类的execute()方法,执行cmd.exe/c ipconfig/all命令,并解析出网卡物理ip地址。getPhysicalAddress()方法返回网卡的物理地址,如果机器中没有安装网卡或操作系统不支持ipconfig命令,返回not find字符串
    import java.io.*;
    import java.util.*;class GetPhysicalAddress{
            //网卡物理地址长度
            static private final int _physicalLength =16;
            
            public static void main(String[] args){               
                    //output you computer phycail ip address
                    System.out.println(getPhysicalAddress());
            }
            
            static public String getPhysicalAddress(){
                    Util_syscmd shell =new Util_syscmd();
                    String cmd = "cmd.exe /c ipconfig/all";
                    Vector result ;
                    result=shell.execute(cmd);  
                    return parseCmd(result.toString());      
            }
            
            //从字符串中解析出所需要获得的字符串
            static private String parseCmd(String s){
                    String find="Physical Address. . . . . . . . . :";
                    int findIndex=s.indexOf(find);
                    if (findIndex==-1)
                            return "not find";
                    else
                            return s.substring(findIndex+find.length()+1,findIndex+find.length()+1+_physicalLength);
                    
                    
            }
    }
      

  3.   

    有跨平台的方法吗?如果是WinXP,Linux,UNIX,Mac怎么办?
      

  4.   

    用jni调用dll,然后用vc写个取网卡mac的dll就可以了。至于vc的写法,到处都是,查一下吧。
      

  5.   

    有一点搞不懂的是这样得到的mac地址是web服务器的网卡物理地址,还是客户机的物理地址?如果是要客户端的网卡物理地址呢?