This article describes the method of obtaining the server IP address and MAC address by Java programming. Share it for your reference, as follows:
Tested system:
windows linux unix
Exclude abnormal IPs such as 127.0.0.1 and 0.0.0.1
import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.util.ArrayList;import java.util.En umeration;import java.util.List;public class IpUtil { private IpUtil() {} /** * This method describes: Get the IP address of the server* @author: [email protected] * @version: September 5, 2014 at 4:57:15 pm */ public static String getLocalIP() { String sIP = ""; InetAddress ip = null; try { boolean bFindIP = false; Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) Ne twinInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { if (bFindIP) { break; } NetworkInterface ni = (NetworkInterface) netInterfaces .nextElement(); Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.ha sMoreElements()) { ip = (InetAddress) ips.nextElement(); if ( !ip.isLoopbackAddress() && ip.getHostAddress().matches( "(//d{1,3}//.){3}//d{1,3}")) { bFindIP = true; break; } } } } } catch (Exception e) { OutUtil.error(IpUtil.class, e.getMessage()); } if (null != ip) { sIP = ip.getHostAddress(); } return sIP; } / ** * This method describes: Obtain the IP address of the server (multiple network card) * @author: [email protected] * @version: September 5, 2014 at 4:57:15 pm */ public static List<String> getLocalIPS () { InemAddress ip = null; List<String> ipList = new ArrayList<String>(); try { Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterf ace>) NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces .nextElement(); Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElement ts()) { ip = (InetAddress) ips.nextElement(); if (!ip. isLoopbackAddress() && ip.getHostAddress().matches( "(//d{1,3}//.){3}//d{1,3}")) { ipList.add(ip.getHostAddress() ); } } } } } catch (Exception e) { OutUtil.error(IpUtil.class, e.getMessage()); } return ipList; } /** * This method describes: obtaining the MAC address of the server* @author : [email protected] * @version: September 5, 2014 at 1:27:25 pm */ public static String getMacId() { String macId = ""; InetAddress ip = null; NetworkInter face ni = null; try { boolean bFindIP = false; Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface .getNetworkInterfaces(); while (netInterfa ces.hasMoreElements()) { if (bFindIP) { break; } ni = (NetworkInterface) netInterfaces .nextElement(); // --------------Specific situations, you can consider using ni.getName to judge// traverse all ips Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ip = (InetAddress) ips.nextElement(); if (!ip.isLoopbackAddress() // Non-127.0.0.1 && ip.getHostAddress().matches( "(//d{1,3}//.){3}/ /d{1,3}")) { bFindIP = true; break; } } } } } catch (Exception e) { OutUtil.error(IpUtil.class, e.getMessage()); } if (null != ip) { try { macId = getMacFromBytes(ni.getHardwareAddress()); } catch (SocketException e) { OutUtil.error(IpUtil.class, e.getMessage()); } } return macId; } /** * This method describes Yes: Get the MAC address of the server (multi-network card) * @author: [email protected] * @version: September 5, 2014 at 1:27:25 pm */ public static List<String> getMacIds() { InetAddress ip = null; NetworkInterface ni = null; List<String> macList = new ArrayList<String>(); try { Enumeration<NetworkInterface> netInterfaces = (Enumeration<Ne twinInterface>) NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { ni = (NetworkInterface) netInterfaces .nextElement(); // -------------- For specific cases, you can consider using ni.getName to judge// traverse all ips Enumeration<InetAddress> ips = ni.getInetAdd resses() ; while (ips.hasMoreElements()) { ip = (InetAddress) ips.nextElement(); if (!ip.isLoopbackAddress() // non-127.0.0.1 && ip.getHostAddress().matches ( "(//d{ 1,3}//.){3}//d{1,3}")) { macList.add(getMacFromBytes(ni.getHardwareAddress())); } } } } } catch (Exception e) { OutUtil.error (IpUtil.class, e.getMessage()); } return macList; } private static String getMacFromBytes(byte[] bytes) { StringBuffer mac = new StringBuffer() ; byte currentByte; boolean first = false; for (byte b : bytes ) { if (first) { mac.append("-"); } currentByte = (byte) ((b & 240) >> 4); mac.append(Integer.toHexString(currentByte)); currentByte = (byte) (b & 15); mac.append(Integer.toHexString(currentByte)); first = true; } return mac.toString().toUpperCase(); }}I hope this article will be helpful to everyone's Java programming.