如何在不同系统下使用 Java 获取系统 IP

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.net.Inet6Address;
import java.net.NetworkInterface;
import java.net.SocketException;
/* 获取IP
* */
public class GetNetworkCardIP {
/*获取本地win的IP*/
private static InetAddress getWinLocalIp() throws UnknownHostException{
InetAddress inet = InetAddress.getLocalHost();
// System.out.println("本机的ip=" + inet.getHostAddress());
return inet;
}
/*获取本地unix的IP*/
private static InetAddress getUnixLocalIp() throws SocketException{
InetAddress ia=null;
try {
Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();
while (e1.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) e1.nextElement();
if (!ni.getName().equals("eth0")) {
continue;
}
else {
Enumeration<?> e2 = ni.getInetAddresses();
while (e2.hasMoreElements()) {
ia = (InetAddress) e2.nextElement();
if (ia instanceof Inet6Address)
{
continue;
}
}
break;
}
}
} catch (SocketException e) {
e.printStackTrace();
System.exit(-1);
}
return ia;
}
/*判断是哪种系统然后获取当前IP*/
public static InetAddress getSystemLocalIP() throws UnknownHostException{
InetAddress inet=null;
String osname=new Getsysinfo().getSysName();
try {
//针对window系统
//if(osname.equalsIgnoreCase("Windows 7")){ 这里是只在"Windows 7"下获取IP
if(osname.startsWith("Windows")){ //这里是在所有Windows系统下下获取IP
inet=getWinLocalIp();
//针对linux系统
}else if(osname.equalsIgnoreCase("Linux")){
inet=getUnixLocalIp();
}
if(null==inet){
throw new UnknownHostException("主机的ip地址未知");
}
}catch (SocketException e) {
throw new UnknownHostException("获取本机ip错误"+e.getMessage());
}
return inet;
}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Java的InetAddress和NetworkInterface类来获取当前系统IP地址、网关和DNS信息。下面是一个示例代码: ```java import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class SystemInfo { public static void main(String[] args) { try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface ni = interfaces.nextElement(); Enumeration<InetAddress> addresses = ni.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (!addr.isLinkLocalAddress() && !addr.isLoopbackAddress() && addr.isSiteLocalAddress()) { System.out.println("IP Address: " + addr.getHostAddress()); } } Enumeration<InetAddress> gateways = ni.getInetAddresses(); while (gateways.hasMoreElements()) { InetAddress addr = gateways.nextElement(); if (!addr.isLinkLocalAddress() && !addr.isLoopbackAddress() && !addr.isSiteLocalAddress()) { System.out.println("Gateway: " + addr.getHostAddress()); } } } InetAddress dns = InetAddress.getByName("8.8.8.8"); System.out.println("DNS: " + dns.getHostAddress()); } catch (SocketException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这个代码会输出当前系统IP地址、网关和DNS信息。请注意,在获取网关地址时,我们需要排除Link-Local和Loopback地址,因为它们不是真正的网关地址。同时,在获取DNS地址时,我们使用了Google Public DNS的IP地址(8.8.8.8),您可以根据需要更改它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值