说明
java获取本机所有ip地址,用下面方法是最好的
从JDK1.4开始,Java提供了 NetworkInterface 类。这个类可以得到 本机所有的物理网络接口 和 虚拟机等软件利用本机的物理网络接口创建的逻辑网络接口 的信息
NetworkInterface 可以通过 getNetworkInterfaces 方法来枚举本机所有的网络接口
NetworkInterface 常用方法
getName方法
public String getName()
这个方法用来得到一个网络接口的名称。这个名称就是使用getByName 方法创建NetworkInterface 对象时使用的网络接口名,如eth0 、ppp0 等。
getDisplayName方法
public String getDisplayName()
这个方法可以得到更容易理解的网络接口名,也可以将这个网络接口名称为网络接口别名。在一些操作系统中(如Unix ),getDisplayName 方法和getName 方法的返回值相同,但在Windows 中getDisplayName 方法一般会返回一个更为友好的名字,如 Realtek RTL8139 Family PCI Fast Ethernet NIC 。
getInetAddresses方法
public Enumeration<InetAddress> getInetAddresses()
NetworkInterface 类可以通过getInetAddresse 方法以InetAddress 对象的形式返回和网络接口绑定的所有IP 地址。getInetAddresses 方法的定义如下:
代码
package top.malaoshi.net;
import java.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class LocalIP {
public static void main(String[] args) throws SocketException, UnknownHostException {
try {
List<String> ips = getIPs();
for(String ip: ips) {
System.out.println("本机IP:" + ip);
}
List<NetInfo> ipAndBroadcast = getNetInfo();
for(NetInfo info : ipAndBroadcast){
System.out.println(info.getIp()+"--"+info.getBroadcast()+"--"+info.getMask());
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
}
public static List<String> getIPs() throws SocketException {
List<String> ips = new ArrayList<>();
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = allNetInterfaces.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
} else {
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress ip = addresses.nextElement();
if (ip != null && ip instanceof Inet4Address) {
ips.add( ip.getHostAddress() );
}
}
}
}
return ips;
}
/**
* 获取ip、广播地址、子网掩码
* @return
* @throws SocketException
*/
public static List<NetInfo> getNetInfo() throws SocketException {
List<NetInfo> ret = new ArrayList<>();
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = allNetInterfaces.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
} else {
List<InterfaceAddress> list = netInterface.getInterfaceAddresses();
for( InterfaceAddress interfaceAddress : list) {
InetAddress inetAddress = interfaceAddress.getAddress();
if (inetAddress != null && inetAddress instanceof Inet4Address) {
String ipAddress = inetAddress.getHostAddress();
InetAddress broadcast = interfaceAddress.getBroadcast();
String broadcastHostAddress = broadcast.getHostAddress();
String mask = parseMask(interfaceAddress.getNetworkPrefixLength());
NetInfo netInfo = new NetInfo(ipAddress,broadcastHostAddress,mask);
ret.add(netInfo);
}
}
}
}
return ret;
}
/**
* 解析子网掩码
* @param mask
* @return
*/
private static String parseMask(short mask){
StringBuilder maskStr = new StringBuilder();
int[] maskIp = new int[4];
for (int i = 0; i < maskIp.length; i++) {
maskIp[i] = (mask >= 8) ? 255 : (mask > 0 ? (mask & 0xff) : 0);
mask -= 8;
maskStr.append(maskIp[i]);
if (i < maskIp.length - 1) {
maskStr.append(".");
}
}
return maskStr.toString();
}
public static class NetInfo {
/**
* ip地址
*/
private String ip;
/**
* 广播地址
*/
private String broadcast;
/**
* 子网掩码
*/
private String mask;
public NetInfo(String ip, String broadcast, String mask) {
this.ip = ip;
this.broadcast = broadcast;
this.mask = mask;
}
@Override
public String toString() {
return "Info{" +
"ip='" + ip + '\'' +
", broadcast='" + broadcast + '\'' +
", mask='" + mask + '\'' +
'}';
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getBroadcast() {
return broadcast;
}
public void setBroadcast(String broadcast) {
this.broadcast = broadcast;
}
public String getMask() {
return mask;
}
public void setMask(String mask) {
this.mask = mask;
}
}
}
参考:
https://blog.csdn.net/nianbingsihan/article/details/80265029