Fork me on GitHub

获取无线局域网适配器IP

RT,嗯!

点击显/隐
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
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class mc_07241 {
public static void main(String[] args) {
getLocalIP();
}
public static List<String> getLocalIP() {
List<String> ipList = new ArrayList<String>();
InetAddress ip = null;
try {
Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
if (ni.getName().indexOf("wlan") == -1)
continue;
// 遍历所有ip
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = (InetAddress) ips.nextElement();
if (null == ip || "".equals(ip)) {
continue;
}
String sIP = ip.getHostAddress();
if (sIP == null || sIP.indexOf(":") > -1) {
continue;
}
ipList.add(sIP);
System.out.println(sIP);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return ipList;
}
}
Your support will encourage me to continue to create!