JAVA如何获取服务器ip

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

InetAddress.getLocalHost().getHostAddress();
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类来实现此功能,如下所示:

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地址:

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地址查询服务的情况。

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2023年12月26日
下一篇 2023年12月26日

相关推荐