Java get native name, native MAC address, IP address

Source: Internet
Author: User
Tags stringbuffer
Package com.howin.util;
 
Import java.net.*; public class Ipconfig {public static void main (string[] args) throws Exception {//TODO auto-generated met
        Hod stub inetaddress ia=null;
             
            try {ia=ia.getlocalhost ();
            String Localname=ia.gethostname ();
            String localip=ia.gethostaddress ();
            SYSTEM.OUT.PRINTLN ("This machine name is:" + localname);
        SYSTEM.OUT.PRINTLN ("The IP of this machine is:" +localip);
        catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); InetAddress ia1 = Inetaddress.getlocalhost ();//Get local IP object System.out.println ("MAC ...).  
    "+getmacaddress (IA1)); //Get MAC Address method private static String getmacaddress (InetAddress ia) throws exception{//Get Network Interface object (that is, NIC) and get  
        To the MAC address, the MAC address exists in a byte array.  
          
        Byte[] mac = Networkinterface.getbyinetaddress (IA). Gethardwareaddress (); The following code is to assemble the MAC address into StrinG StringBuffer sb = new StringBuffer ();  
            for (int i=0;i<mac.length;i++) {if (i!=0) {sb.append ("-");  
            }//mac[i] & 0xFF is to convert the byte to a positive integer String s = integer.tohexstring (Mac[i] & 0xFF);
            System.out.println ("--------------");
            
            System.err.println (s);  
        Sb.append (S.length () ==1?0+s:s);  
    To capitalize all lowercase letters of the string into a regular MAC address and return Sb.tostring (). toUpperCase (); }  
 
}

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.