您的位置:

Java获取IP的方法

一、使用InetAddress获取IP地址

在Java中获取本地IP地址最简单的方式是使用InetAddress类。该类提供了一个静态方法getLocalHost(),当调用该方法时,会返回一个InetAddress实例,可以使用该实例的getHostAddress()方法获取IP地址。以下是示例代码:

import java.net.InetAddress;
 
public class GetLocalIpAddress {
    public static void main(String[] args) throws Exception {
        InetAddress address = InetAddress.getLocalHost();
        String ipAddress = address.getHostAddress();
        System.out.println("Local IP Address: " + ipAddress);
    }
}

运行以上代码会输出类似如下内容:
Local IP Address: 192.168.1.101

二、使用NetworkInterface获取IP地址

另一种获取本地IP地址的方式是使用Java中的NetworkInterface类。该类提供了getNetworkInterfaces()方法来获取所有网络接口的列表,包括IP地址、子网掩码、网关等信息。以下是示例代码:

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
 
public class GetLocalIpAddress {
    public static void main(String[] args) throws Exception {
        Enumeration networkInterface = NetworkInterface.getNetworkInterfaces();
        while (networkInterface.hasMoreElements()) {
            NetworkInterface ni = networkInterface.nextElement();
            Enumeration
    inetAddresses = ni.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress address = inetAddresses.nextElement();
                System.out.println("IP Address: " + address.getHostAddress());
            }
        }
    }
}

   
  

运行以上代码会输出类似如下内容:
IP Address: 192.168.1.101

三、使用Servlet获取IP地址

在web应用程序中,可以通过HttpServletRequest对象获取客户端的IP地址。以下是示例代码:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class GetClientIpAddressServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String ipAddress = request.getHeader("X-Forwarded-For");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
        }
        System.out.println("Client IP Address: " + ipAddress);
    }
}

上述代码中使用了一些常用的HTTP头信息来获取客户端IP地址。运行以上代码可以在控制台输出客户端IP地址。

四、总结

上述三种方式均可以获取本地IP地址和客户端IP地址。其中,InetAddress方式简单易用,但只能获取本机IP地址,无法获取远程IP地址;NetworkInterface方式比较底层,可以获取本机所有接口的IP地址,包括局域网和广域网;Servlet方式只能获取客户端IP地址,但需要在web应用程序中使用。