java百分百获取到机器IP地址及MAC码ITeye - 乐橙lc8

java百分百获取到机器IP地址及MAC码ITeye

2019-01-12 06:48:50 | 作者: 迎丝 | 标签: 地址,办法,描绘 | 浏览: 2951

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; * 此办法描绘的是:取得效劳器的IP地址(多网卡) * @author: zhangyang33@sinopharm.com * @version: 2014年 下午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; * 此办法描绘的是:取得效劳器的MAC地址 * @author: zhangyang33@sinopharm.com * @version: 2014年 下午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; * 此办法描绘的是:取得效劳器的MAC地址(多网卡) * @author: zhangyang33@sinopharm.com * @version: 2014年 下午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(); }

来自:http://jdkleo.iteye.com/blog/2113782博客

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表乐橙lc8立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章