java编程实现获取服务器IP地址及MAC地址的方法

这篇文章主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下

本文实例讲述了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精讲视频教程,如果你现在也在学习Java,在入门学习Java的过程当中缺乏系统的学习教程,你可以加我的Java学习交流群:494801931,获取,群文件还有学习手册,面试题,开发工具,PDF文档教程,可以自行下载。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,640评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,254评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,011评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,755评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,774评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,610评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,352评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,257评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,717评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,894评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,021评论 1 350
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,735评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,354评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,936评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,054评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,224评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,974评论 2 355

推荐阅读更多精彩内容