ImportJava.net.NetworkInterface;Importjava.net.SocketException;Importjava.util.Enumeration;Importorg.apache.commons.codec.digest.DigestUtils;Importcom.yd.common.utils.StringUtils;/** * * @authorMrwang * Get client MAC address * **/ Public classMacAddress {Private StaticString mac_s = ""; Static { if(mac_s = = "") {Enumeration<NetworkInterface>el; Try{El=networkinterface.getnetworkinterfaces (); while(El.hasmoreelements ()) {NetworkInterface NetworkInterface=el.nextelement (); byte[] Mac =networkinterface.gethardwareaddress (); if(Mac = =NULL|| Mac.length==0) Continue; if(mac.length==6) {mac_s= HexByte (mac[0]) + "-" + hexbyte (mac[1]) + "-" + hexbyte (mac[2]) + "-" + hexbyte (mac[3]) + "-" + hexbyte (mac[4]) + "-" + hexbyte (mac[5]); System.out.println (mac_s); //mac_s = Digestutils.md5hex (mac_s). toUpperCase (). substring (0, 8); //System.out.println (mac_s); Break; } } } Catch(socketexception E1) {e1.printstacktrace (); } } } Public StaticString HexByte (byteb) {String s= "00" +integer.tohexstring (b); returnS.substring (S.length ()-2); } Public StaticString Getmac () {returnmac_s; } Public Static voidMain (string[] args) {MacAddress m=NewMacAddress (); M.getmac (); }}
Get MAC Address