java如何获取服务器ip
-
在Java中,可以通过以下几种方式来获取服务器的IP:
-
使用InetAddress类:
import java.net.InetAddress; public class GetServerIP { public static void main(String[] args) { try { InetAddress address = InetAddress.getLocalHost(); String ip = address.getHostAddress(); System.out.println("服务器IP地址:" + ip); } catch (Exception e) { e.printStackTrace(); } } }该程序使用
getLocalHost()方法获取本地主机的InetAddress对象,并通过getHostAddress()方法获取IP地址。 -
使用NetworkInterface类:
import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; public class GetServerIP { public static void main(String[] args) { try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) { System.out.println("服务器IP地址:" + address.getHostAddress()); } } } } catch (Exception e) { e.printStackTrace(); } } }该程序使用
getNetworkInterfaces()方法获取所有网络接口的枚举,然后遍历每个网络接口,并使用getInetAddresses()方法获取该网络接口的所有IP地址。 -
使用System类和Environment变量:
public class GetServerIP { public static void main(String[] args) { String ip = System.getenv("SERVER_IP"); System.out.println("服务器IP地址:" + ip); } }该程序使用
getenv()方法获取名为"SERVER_IP"的环境变量的值。
通过以上方法,可以获取到服务器的IP地址。需要注意的是,第一种方式只能获取到本地主机的IP地址,而第二种方式可以获取到所有网络接口的IP地址,所以推荐使用第二种方式来获取服务器的IP地址。
1年前 -
-
要获取服务器的IP地址,可以使用Java的InetAddress类和NetworkInterface类。
- 使用InetAddress类获取本机的IP地址:
import java.net.InetAddress; public class GetLocalIpAddress { public static void main(String[] args) { try { InetAddress localHost = InetAddress.getLocalHost(); String ipAddress = localHost.getHostAddress(); System.out.println("本机IP地址:" + ipAddress); } catch (Exception e) { e.printStackTrace(); } } }- 使用InetAddress类获取指定域名的IP地址:
import java.net.InetAddress; public class GetServerIpAddress { public static void main(String[] args) { try { String domainName = "www.example.com"; // 替换为实际域名 InetAddress[] serverAddresses = InetAddress.getAllByName(domainName); for (InetAddress address : serverAddresses) { System.out.println("域名:" + domainName); System.out.println("IP地址:" + address.getHostAddress()); } } catch (Exception e) { e.printStackTrace(); } } }- 使用NetworkInterface类获取本机的所有网络接口信息:
import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class GetNetworkInterfaceInfo { public static void main(String[] args) { try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); System.out.println("接口名:" + networkInterface.getDisplayName()); System.out.println("接口MAC地址:" + networkInterface.getHardwareAddress()); Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); System.out.println("IP地址:" + address.getHostAddress()); } System.out.println(); } } catch (SocketException e) { e.printStackTrace(); } } }- 使用NetworkInterface类获取指定网络接口的IP地址:
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class GetInterfaceIpAddress { public static void main(String[] args) { try { String interfaceName = "eth0"; // 替换为实际接口名 NetworkInterface networkInterface = NetworkInterface.getByName(interfaceName); Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); System.out.println("接口名:" + networkInterface.getDisplayName()); System.out.println("接口MAC地址:" + networkInterface.getHardwareAddress()); System.out.println("IP地址:" + address.getHostAddress()); System.out.println(); } } catch (SocketException e) { e.printStackTrace(); } } }- 使用外部库(如Apache Commons Net库)获取指定域名的IP地址:
import org.apache.commons.net.util.SubnetUtils; public class GetServerIpAddress { public static void main(String[] args) { try { String domainName = "www.example.com"; // 替换为实际域名 SubnetUtils subnetUtils = new SubnetUtils(domainName); String[] ipAddresses = subnetUtils.getInfo().getAllAddresses(); for (String ipAddress : ipAddresses) { System.out.println("域名:" + domainName); System.out.println("IP地址:" + ipAddress); } } catch (Exception e) { e.printStackTrace(); } } }以上是几种获取服务器IP地址的方法,可以根据具体需求选择合适的方法进行使用。
1年前 -
Java获取服务器IP地址的方法有很多种,包括使用Socket、InetAddress和NetworkInterface等类实现。下面将分别详细介绍这几种方法的操作流程。
方法一:使用Socket类
通过创建Socket对象连接服务器,然后使用getInetAddress方法获取IP地址。import java.net.InetAddress; import java.net.Socket; public class GetServerIPUsingSocket { public static void main(String[] args) { try { Socket socket = new Socket("服务器地址", 端口号); InetAddress serverAddress = socket.getInetAddress(); System.out.println("服务器IP地址:" + serverAddress.getHostAddress()); socket.close(); } catch (Exception e) { e.printStackTrace(); } } }方法二:使用InetAddress类
InetAddress类是Java提供的用于表示IP地址的类,通过调用其getByName方法获取IP地址。import java.net.InetAddress; public class GetServerIPUsingInetAddress { public static void main(String[] args) { try { InetAddress serverAddress = InetAddress.getByName("服务器地址"); System.out.println("服务器IP地址:" + serverAddress.getHostAddress()); } catch (Exception e) { e.printStackTrace(); } } }方法三:使用NetworkInterface类
NetworkInterface类提供了获取网络接口信息的方法,通过调用其getInetAddresses方法获取IP地址。import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; public class GetServerIPUsingNetworkInterface { public static void main(String[] args) { try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().indexOf(":") == -1) { System.out.println("服务器IP地址:" + inetAddress.getHostAddress()); } } } } catch (Exception e) { e.printStackTrace(); } } }通过上述三种方法,我们可以在Java中获取服务器IP地址。其中,方法一使用Socket类需要先连接服务器,所以适用于已经知道服务器地址和端口号的情况;方法二使用InetAddress类更加简洁,适用于直接获取指定服务器的IP地址;方法三使用NetworkInterface类可以获取本机的所有IP地址,适用于获取本机的局域网IP地址。根据需求选择合适的方法即可。
1年前