一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java編程實現獲取服務器IP地址及MAC地址的方法

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

2020-01-21 15:09jdkleo JAVA教程

這篇文章主要介紹了java編程實現獲取機器IP地址及MAC地址的方法,實例分析了Java分別針對單網卡及多網卡的情況下獲取服務器IP地址與MAC地址的相關技巧,需要的朋友可以參考下

本文實例講述了java編程實現獲取服務器IP地址及MAC地址的方法。分享給大家供大家參考,具體如下:

已測系統:
windows linux unix

排除127.0.0.1 和 0.0.0.0.1等非正常IP

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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: [email protected]
  * @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: [email protected]
  * @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: [email protected]
  * @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: [email protected]
  * @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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 翁公与小莹在客厅激情 | 四虎院影永久在线观看 | 女同69式互添在线观看免费 | 亚洲国产成人精品 | 特级淫片大乳女子高清视频 | 9久爱午夜视频 | 小嫩videos| 久久99亚洲热最新地址获取 | 免费看a片毛片 | 久久中文字幕无线观看 | 国产高清精品自在久久 | 性趣用品 | а天堂中文最新版在线官网视频 | 欧式午夜理伦三级在线观看 | 欧美日韩一二三区免费视频观看 | 久久视频这里只精品99热在线观看 | 欧美福利在线观看 | 久久视频精品3线视频在线观看 | 免费网站直接进入 | 小货SAO边洗澡边CAO你动漫 | 国产精亚洲视频 | 久久99精国产一区二区三区四区 | 波多野结衣在线观看中文字幕 | 免费在线观看亚洲 | 日本三级欧美三级人妇英文 | 亚洲精品久久玖玖玖玖 | 国产日韩欧美精品在线 | 国产青草亚洲香蕉精品久久 | 色在线看 | 久久成人免费大片 | 国产精品一二三 | 国产3p在线| 羞羞麻豆国产精品1区2区3区 | 日韩永久在线观看免费视频 | 亚洲熟区 | 久久久精品免费视频 | 亚洲精品成人456在线播放 | 草逼视频网址 | 亚洲国产精久久久久久久 | haodiaocao几万部精彩视频 | 国产免费午夜高清 |