您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)java中怎么獲取機(jī)器IP地址及MAC碼,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
java百分百獲取到機(jī)器IP地址及MAC碼
已測(cè)系統(tǒng):
windows linux unix
排除127.0.0.1 和 0.0.0.0.1等非正常IP
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; public class IpUtil { private IpUtil(){} /** * 此方法描述的是:獲得服務(wù)器的IP地址 * @author: zhangyang33@sinopharm.com * @version: 2014年9月5日 下午4:57:15 */ public static String getLocalIP() { String sIP = ""; InetAddress ip = null; try { boolean bFindIP = false; Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { if (bFindIP) { break; } NetworkInterface ni = (NetworkInterface) netInterfaces .nextElement(); Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { 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; } /** * 此方法描述的是:獲得服務(wù)器的IP地址(多網(wǎng)卡) * @author: zhangyang33@sinopharm.com * @version: 2014年9月5日 下午4:57:15 */ public static List<String> getLocalIPS() { InetAddress ip = null; List<String> ipList = new ArrayList<String>(); try { Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces .nextElement(); Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { 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; } /** * 此方法描述的是:獲得服務(wù)器的MAC地址 * @author: zhangyang33@sinopharm.com * @version: 2014年9月5日 下午1:27:25 */ public static String getMacId() { String macId = ""; InetAddress ip = null; NetworkInterface ni = null; try { boolean bFindIP = false; Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { if (bFindIP) { break; } ni = (NetworkInterface) netInterfaces .nextElement(); // ----------特定情況,可以考慮用ni.getName判斷 // 遍歷所有ip Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ip = (InetAddress) ips.nextElement(); if (!ip.isLoopbackAddress() // 非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; } /** * 此方法描述的是:獲得服務(wù)器的MAC地址(多網(wǎng)卡) * @author: zhangyang33@sinopharm.com * @version: 2014年9月5日 下午1:27:25 */ public static List<String> getMacIds() { InetAddress ip = null; NetworkInterface ni = null; List<String> macList = new ArrayList<String>(); try { Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface .getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { ni = (NetworkInterface) netInterfaces .nextElement(); // ----------特定情況,可以考慮用ni.getName判斷 // 遍歷所有ip Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ip = (InetAddress) ips.nextElement(); if (!ip.isLoopbackAddress() // 非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(); } }
關(guān)于java中怎么獲取機(jī)器IP地址及MAC碼就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。