Java gets MAC address based on local IP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.