1 Importjava.net.InetAddress;2 ImportJava.net.NetworkInterface;3 Importjava.net.SocketException;4 Importjava.net.UnknownHostException;5 6 Public classMacAddress {7 8 /**9 * @paramargsTen * @throwsunknownhostexception One * @throwssocketexception A */ - Public Static voidMain (string[] args)throwsunknownhostexception, socketexception { - theInetAddress ia =inetaddress.getlocalhost (); - System.out.println (IA); - Getlocalmac (IA); - } + Private Static voidGetlocalmac (InetAddress ia)throwsSocketException { - //TODO auto-generated Method Stub + //get the network card, get the address A byte[] Mac =Networkinterface.getbyinetaddress (IA). gethardwareaddress (); at -System.out.println ("Mac array length:" +mac.length); -StringBuffer SB =NewStringBuffer (""); - for(inti=0; i<mac.length; i++) { - if(i!=0) { -Sb.append ("-"); in } - //bytes converted to integers to inttemp = Mac[i]&0xff; +String str =integer.tohexstring (temp); -System.out.println ("Every 8 bits:" +str); the if(Str.length () ==1) { *Sb.append ("0" +str); $}Else {Panax Notoginseng sb.append (str); - } the } +System.out.println ("Native MAC address:" +sb.tostring (). toUpperCase ()); A } the}
Originally wanted to write a Web program by obtaining a user IP and then get the user's network card address, so that you can do some business ... Results found that the web is not implemented temporarily, find another way to
Java gets MAC address based on local IP