您现在的位置是:首页 >

电脑识别不了网卡怎么解决 Java实现读取本机网卡Mac地址

火烧 2021-05-20 12:12:45 1094
Java实现读取本机网卡Mac地址   本方法主要用来限制系统在其他的机器上运行 其实原理简单的很 没有调用第三方插件 代码如下:   ackage u er util   im ort java i

Java实现读取本机网卡Mac地址  

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

  package users util;

  import    java io *;

  import    java util *;

  import    java util regex *;

  public    class    NetID    {

  String    IPCONFIG_MAND_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_MAND_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    <=    )    {

  continue;

  }

  String    macAddressCandidate    =    line substring(macAddressPosition    +    )

  trim();

  if    (isMacAddWin(macAddressCandidate))    {

  if    (realMac    ==    true)    {

  generateUnique(macAddressCandidate);

  }

  else    {

  realMac    =    true;

  }

  }

  }

  return    unique;

  }

  private    String    runConsoleCommand(String    mand)    throws    IOException    {

  Process    p    =    Runtime getRuntime() exec(mand);

  InputStream    stdoutStream    =    new    BufferedInputStream(p getInputStream());

  StringBuffer    buffer    =    new    StringBuffer();

  while    (true)    {

  int    c    =    stdoutStream read();

  if    (c    ==    )    {

  break;

电脑识别不了网卡怎么解决 Java实现读取本机网卡Mac地址

  }

  buffer append(    (char)    c);

  }

  String    outputText    =    buffer toString();

  stdoutStream close();

  return    outputText;

  }

  private    boolean    isMacAddWin(String    macAddressCandidate)    {

  Pattern    macPattern    =    pile( [ a fA F]{ } [ a fA F]{ } [ a fA F]{ } [ a fA F]{ } [ a fA F]{ } [ a fA F]{ } );

  Matcher    m    =    macPattern matcher(macAddressCandidate);

  return    m matches();

  }

  private    boolean    isMacAddOSX(String    macAddressCandidate)    {

  if    (macAddressCandidate length()    !=    )    {

  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());

  }

lishixinzhi/Article/program/Java/hx/201311/25985  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码