国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java編程實現獲取服務器IP地址及MAC地址的方法

2019-11-26 14:50:36
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 察雅县| 微山县| 翁牛特旗| 大田县| 荆州市| 自贡市| 内丘县| 微山县| 通山县| 温泉县| 无极县| 肇东市| 金华市| 荆州市| 晋江市| 衢州市| 岗巴县| 英山县| 咸宁市| 乌恰县| 陵水| 凤凰县| 玉山县| 子洲县| 沛县| 焦作市| 碌曲县| 肇源县| 商城县| 罗源县| 福安市| 大同市| 广昌县| 成安县| 南江县| 招远市| 南城县| 邓州市| 岳普湖县| 鸡西市| 涞源县|