Last active
January 12, 2023 01:51
-
-
Save pfmiles/9f7273b12b4b486e2e9c to your computer and use it in GitHub Desktop.
java中获取本机ip列表
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.net.InetAddress; | |
import java.net.NetworkInterface; | |
import java.net.SocketException; | |
import java.util.Enumeration; | |
import java.util.HashSet; | |
import java.util.Iterator; | |
import java.util.Set; | |
/** | |
* 获取本地ip的工具 | |
* | |
* @author pf-miles | |
* @since 2014-11-25 | |
*/ | |
public class LocalIpAddressUtil { | |
/** | |
* 获取本地ip地址,有可能会有多个地址, 若有多个网卡则会搜集多个网卡的ip地址 | |
*/ | |
public static Set<InetAddress> resolveLocalAddresses() { | |
Set<InetAddress> addrs = new HashSet<InetAddress>(); | |
Enumeration<NetworkInterface> ns = null; | |
try { | |
ns = NetworkInterface.getNetworkInterfaces(); | |
} catch (SocketException e) { | |
// ignored... | |
} | |
while (ns != null && ns.hasMoreElements()) { | |
NetworkInterface n = ns.nextElement(); | |
Enumeration<InetAddress> is = n.getInetAddresses(); | |
while (is.hasMoreElements()) { | |
InetAddress i = is.nextElement(); | |
if (!i.isLoopbackAddress() && !i.isLinkLocalAddress() && !i.isMulticastAddress() | |
&& !isSpecialIp(i.getHostAddress())) addrs.add(i); | |
} | |
} | |
return addrs; | |
} | |
public static Set<String> resolveLocalIps() { | |
Set<InetAddress> addrs = resolveLocalAddresses(); | |
Set<String> ret = new HashSet<String>(); | |
for (InetAddress addr : addrs) | |
ret.add(addr.getHostAddress()); | |
return ret; | |
} | |
/** | |
* 取得当前机器的address信息,若有多个则随机选一个 | |
*/ | |
public static InetAddress resolveLocalAddress() { | |
Set<InetAddress> addrs = resolveLocalAddresses(); | |
if (addrs != null && !addrs.isEmpty()) return addrs.iterator().next(); | |
return null; | |
} | |
private static boolean isSpecialIp(String ip) { | |
if (ip.contains(":")) return true; | |
if (ip.startsWith("127.")) return true; | |
if (ip.startsWith("169.254.")) return true; | |
if (ip.equals("255.255.255.255")) return true; | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment