I found a way to get the MAC address on the Internet, and I found two different methods.
First Kind
public static void Main (string[] args) throws Exception {
inetaddress ia = Inetaddress.getlocalhost ();
System.out.println (Getmacaddress (IA));
}
private static String Getmacaddress (InetAddress ia) throws Exception {
//Gets the network interface object (that is, the NIC) and gets the MAC address, which exists in a byte array.
byte[] mac = Networkinterface.getbyinetaddress (IA). gethardwareaddress ();
The following code is to assemble the MAC address into a 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);
Sb.append (s.length () = = 1 0 + s:s);
Change all lowercase letters of the string to uppercase to become a regular MAC address and return
sb.tostring (). toUpperCase ();
This method looks like it can only take the MAC address of the machine.
Second Kind
public static void Main (string[] args) throws Exception {
Getmac ("192.168.1.186");
public static string Getmac (string IP) {
string str = null;
String mac = null;
try{
Process p = runtime.getruntime (). EXEC ("nbtstat-a" + IP);
InputStreamReader ir = new InputStreamReader (P.getinputstream (), "GBK");
LineNumberReader input = new LineNumberReader (IR);
for (; true;) {
str = input.readline ();
if (str!= null) {
if (Str.indexof ("MAC Address") > 1) {
Mac = str.substring (Str.indexof ("MAC address") + 9);
break;
}
}} System.out.println (MAC);
catch (IOException e) {
e.printstacktrace ();
}
return mac;
}
I prefer this method, but it may be a little less efficient in time. This has a comparison to note that the data flow there to remember to change to GBK format, otherwise read out of the data is garbled, the back will not be able to, and then identify the field there, there may be some will be "MAC address", so you may need to make some adjustments.
The above is a small series of Java to get the MAC address of the two methods (recommended) All content, I hope that we support cloud Habitat Community ~