手机
当前位置:查字典教程网 >编程开发 >Java >java编程实现获取服务器IP地址及MAC地址的方法
java编程实现获取服务器IP地址及MAC地址的方法
摘要:本文实例讲述了java编程实现获取服务器IP地址及MAC地址的方法。分享给大家供大家参考,具体如下:已测系统:windowslinuxuni...

本文实例讲述了java编程实现获取服务器IP地址及MAC地址的方法。分享给大家供大家参考,具体如下:

已测系统:

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(){} /** * 此方法描述的是:获得服务器的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; } /** * 此方法描述的是:获得服务器的IP地址(多网卡) * @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; } /** * 此方法描述的是:获得服务器的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; } /** * 此方法描述的是:获得服务器的MAC地址(多网卡) * @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(); } }

希望本文所述对大家Java程序设计有所帮助。

【java编程实现获取服务器IP地址及MAC地址的方法】相关文章:

java获取web容器地址的方法

使用java获取md5值的两种方法

java获取日期的方法

Java 中实现随机无重复数字的方法

Java多线程下载的实现方法

Java 图片压缩实现思路及代码

基于序列化存取实现java对象深度克隆的方法详解

java中使用sax解析xml的解决方法

java配置数据库连接池的方法步骤

java el简介及用法

精品推荐
分类导航