Chinaunix首页 | 论坛 | 博客
  • 博客访问: 60133
  • 博文数量: 44
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 470
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-02 23:48
文章分类

全部博文(44)

文章存档

2009年(44)

我的朋友
最近访客

分类: Java

2009-08-18 10:13:04

本方法主要用来限制系统在其他的机器上运行.其实原理简单的很.没有调用第三方插件.代码如下:

package com.users.util;

   import    java.io.*;   
   import    java.util.*;   
   import    java.util.regex.*;   
    
   public    class    NetID    {   
       String    IPCONFIG_COMMAND_WIN    =    "ipconfig    /all";   
       boolean    realMac    =    true;   
       String    unique    =    "";   
    
       public    static    String    getMacAddress()    {   
           NetID    hwid    =    new    NetID();   
           return    hwid.getUnique().trim();   
       }   
    
       private    String    getUnique()    {   
           String    os    =    System.getProperty("os.name");   
    
           if    (os.startsWith("Windows"))    {   
               return    getUniqueWindows();   
           }else    {   
               return    "";   
           }   
       }   
    
       private    String    getUniqueWindows()    {   

           String    ipConfigResponse    =    null;   
           try    {   
               ipConfigResponse    =    runConsoleCommand(IPCONFIG_COMMAND_WIN);   
           }   
           catch    (IOException    e)    {   
               e.printStackTrace();   
           }   
    

           StringTokenizer    tokenizer    =    new    StringTokenizer(ipConfigResponse,    "\n");   
           while    (tokenizer.hasMoreTokens())    {   
               String    line    =    tokenizer.nextToken().trim();   
    

               int    macAddressPosition    =    line.indexOf(":");   
               if    (macAddressPosition    <=    0)    {   
                   continue;   
               }   
               String    macAddressCandidate    =    line.substring(macAddressPosition    +    1).   
                       trim();     
               if    (isMacAddWin(macAddressCandidate))    {   
                   if    (realMac    ==    true)    {   
                       generateUnique(macAddressCandidate);   
                   }   
                   else    {   
                       realMac    =    true;   
                   }   
               }   
           }   
    
           return    unique;   
       }   
    
    

       private    String    runConsoleCommand(String    command)    throws    IOException    {   
           Process    p    =    Runtime.getRuntime().exec(command);   
           InputStream    stdoutStream    =    new    BufferedInputStream(p.getInputStream());   
    
           StringBuffer    buffer    =    new    StringBuffer();   
           while    (true)    {   
               int    c    =    stdoutStream.read();   
               if    (c    ==    -1)    {   
                   break;   
               }   
               buffer.append(    (char)    c);   
           }   
           String    outputText    =    buffer.toString();   
    
           stdoutStream.close();   
    
           return    outputText;   
       }   
    

       private    boolean    isMacAddWin(String    macAddressCandidate)    {   
           Pattern    macPattern    =    Pattern.compile("[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}");   
           Matcher    m    =    macPattern.matcher(macAddressCandidate);   
           return    m.matches();   
       }   
    

       private    boolean    isMacAddOSX(String    macAddressCandidate)    {   
           if    (macAddressCandidate.length()    !=    17)    {   
               return    false;   
           }   
           else    {   
               return    true;   
           }   
       }   
    

       private    void    generateUnique(String    macAddress)    {   
           if    (unique    ==    "")    {   
               unique    +=    macAddress;   
           }   
           else    {   
               unique    +=    "#";   
               unique    +=    macAddress;   
           }   
       }   
    
       public    static    void    main(String [] args)    {   
           System.out.println(NetID.getMacAddress());   
       }   
   }
阅读(396) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~