JAVA如何获取服务器ip

一、最简单的方法就是使用InetAddress获取本机ip

复制代码
InetAddress.getLocalHost().getHostAddress();
java 复制代码
public static void main(String[] args) {
        try {
            //用 getLocalHost() 方法创建的InetAddress的对象
            InetAddress address = InetAddress.getLocalHost();
            System.out.println(address.getHostName());//主机名
            System.out.println(address.getCanonicalHostName());//主机别名
            System.out.println(address.getHostAddress());//获取IP地址
            System.out.println("===============");
            
            //用域名创建 InetAddress对象
            InetAddress address1 = InetAddress.getByName("www.wodexiangce.cn");
            //获取的是该网站的ip地址,如果我们所有的请求都通过nginx的,所以这里获取到的其实是nginx服务器的IP地址
            System.out.println(address1.getHostName());//www.wodexiangce.cn
            System.out.println(address1.getCanonicalHostName());//124.237.121.122
            System.out.println(address1.getHostAddress());//124.237.121.122
            System.out.println("===============");
            
            //用IP地址创建InetAddress对象
            InetAddress address2 = InetAddress.getByName("220.181.111.188");
            System.out.println(address2.getHostName());//220.181.111.188
            System.out.println(address2.getCanonicalHostName());//220.181.111.188
            System.out.println(address2.getHostAddress());//220.181.111.188
            System.out.println("===============");
            
            //根据主机名返回其可能的所有InetAddress对象
            InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");
            for (InetAddress addr : addresses) {
                System.out.println(addr);
                //www.baidu.com/220.181.111.188
                //www.baidu.com/220.181.112.244
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

二、当服务器有多个网络接口或配置了多个IP地址时

该方法返回的是默认的本地地址,可能是服务器上某个网络接口的IP地址,但不一定是我们期望获取的IP地址。

为了获取正确的IP地址,可以使用其他方法来获取服务器上所有的网络接口,并遍历每个网络接口来获取对应的IP地址。可以使用NetworkInterface类来实现此功能,如下所示:

java 复制代码
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class GetServerIPAddress {
    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.isLinkLocalAddress() && address.isSiteLocalAddress()) {
                        System.out.println("服务器的IP地址是:" + address.getHostAddress());
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
}

上述代码中,我们使用NetworkInterface.getNetworkInterfaces()方法获取所有的网络接口,然后遍历每个网络接口,再通过getInetAddresses()方法获取每个网络接口的IP地址。在获取IP地址时,我们可以根据需求进行过滤,例如排除回环地址、链路本地地址,只选择站点本地地址等。

通过这种方式,我们可以获取到服务器上所有网络接口的IP地址,包括多个IP地址的情况。这样可以确保获取到正确的IP地址。

三、当使用代理服务器时

当使用代理服务器时,InetAddress.getLocalHost()方法返回的是本地主机(即运行该代码的主机)的IP地址,而不是代理服务器的IP地址。这是因为getLocalHost()方法返回的是当前主机的IP地址,而不是代理服务器的IP地址。

在使用代理服务器时,如果想要获取代理服务器的IP地址,可以使用其他方法来实现,例如可以发送一个HTTP请求到一个公共的IP地址查询服务,然后从返回的响应中解析出代理服务器的IP地址。

以下是一个示例代码,演示如何通过发送HTTP请求获取代理服务器的IP地址:

java 复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;

public class GetProxyIPAddress {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://api.ipify.org");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String ipAddress = reader.readLine();
                System.out.println("代理服务器的IP地址是:" + ipAddress);
                reader.close();
            }
            connection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

上述代码中,我们发送一个GET请求到http://api.ipify.org,该服务会返回我们的公共IP地址。通过解析响应,我们可以获取到代理服务器的IP地址。请注意,这种方式仅适用于能够访问公共IP地址查询服务的情况。

相关推荐
wheeldown17 小时前
【Linux】 存储分级的秘密
linux·运维·服务器
月阳羊17 小时前
【硬件-笔试面试题-95】硬件/电子工程师,笔试面试题(知识点:RC电路中的时间常数)
java·经验分享·单片机·嵌入式硬件·面试
Bigemap17 小时前
BigemapPro快速添加历史影像(Arcgis卫星地图历史地图)
java·开发语言
IT学长编程17 小时前
计算机毕业设计 基于Hadoop的健康饮食推荐系统的设计与实现 Java 大数据毕业设计 Hadoop毕业设计选题【附源码+文档报告+安装调试】
java·大数据·hadoop·毕业设计·课程设计·推荐算法·毕业论文
hrrrrb17 小时前
【Python】字符串
java·前端·python
mkhase17 小时前
9.12-QT-基本登陆界面实现
java·jvm·qt
yinke小琪17 小时前
说说hashCode() 和 equals() 之间的关系
java·后端·面试
若鱼191917 小时前
Kafka如何配置生产者拦截器和消费者拦截器
java·kafka
渣哥17 小时前
Java 自适应自旋锁机制详解:原理、优缺点与应用场景
java
摇滚侠18 小时前
java语言中,list<String>转成字符串,逗号分割;List<Integer>转字符串,逗号分割
java·windows·list