如何在 Java HttpServletRequest 中获取客户端 Ip 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29910074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:48:18  来源:igfitidea点击:

How to get client Ip Address in Java HttpServletRequest

javaservletsip

提问by Samith Dilshan

I am trying to develop a Java web application (Servlet) which I need to get clients IP address.

我正在尝试开发一个需要获取客户端 IP 地址的 Java Web 应用程序 (Servlet)。

Following is my code so far:

以下是我到目前为止的代码:

String ipAddress =  request.getRemoteAddr();

In this case most of the time I get the 'Default gateway address' (147.120.1.5). Not my machine IP address(174.120.100.17).

在这种情况下,大部分时间我都会得到“默认网关地址”(147.120.1.5)。不是我的机器 IP 地址 (174.120.100.17)。

String ipAddress = request.getHeader("X-FORWARDED-FOR");  
if (ipAddress == null) {  
    ipAddress = request.getRemoteAddr();  
} 

In this case most of the time I get the 'Default gateway address' (147.120.1.5). Not my machine IP address (174.120.100.17).

在这种情况下,大部分时间我都会得到“默认网关地址”(147.120.1.5)。不是我的机器 IP 地址 (174.120.100.17)。

InetAddress IP=InetAddress.getLocalHost();
System.out.println(IP.getHostAddress());

In this case I got the server IP Address (147.120.20.1).

在这种情况下,我得到了服务器 IP 地址 (147.120.20.1)。

My IP address in 147.120.100.17. Now I don't know how to get the real client IP address.

我的 IP 地址在 147.120.100.17。现在我不知道如何获得真实的客户端IP地址。

Thank you very much.

非常感谢。

采纳答案by gihan-maduranga

Try this one,

试试这个,

String ipAddress = request.getHeader("X-FORWARDED-FOR");  
if (ipAddress == null) {  
    ipAddress = request.getRemoteAddr();  
}

reference : http://www.mkyong.com/java/how-to-get-client-ip-address-in-java/

参考:http: //www.mkyong.com/java/how-to-get-client-ip-address-in-java/

回答by bhargav

 import java.net.UnknownHostException;

/**
 * Simple Java program to find IP Address of localhost. This program uses
 * InetAddress from java.net package to find IP address.
 *
 */
public class IPTest { 

public static void main(String args[]) throws UnknownHostException {

    InetAddress addr = InetAddress.getLocalHost();

    //Getting IPAddress of localhost - getHostAddress return IP Address
    // in textual format
    String ipAddress = addr.getHostAddress();

    System.out.println("IP address of localhost from Java Program: " + ipAddress);

    //Hostname
    String hostname = addr.getHostName();
    System.out.println("Name of hostname : " + hostname);     
}
}

Output:

输出:

IP address of localhost from Java Program: 190.12.209.123
Name of hostname : PCLOND3433

回答by Partha Mondal

In case, you are trying to get the IP-address for Dev-environment then you can use this:-

如果您正在尝试获取开发环境的 IP 地址,那么您可以使用:-

public String processRegistrationForm(HttpServletRequest request)
{
    String appUrl = request.getScheme() + "://"+ request.getLocalAddr();
    return appUrl;
}

The request.getLocalAddr()will return the IP-address of the request receiving system.

request.getLocalAddr()会返回请求接收系统的IP地址。

Hope it helps.

希望能帮助到你。

回答by elaheh aghaee

Try this one. for all condition

试试这个。对于所有条件

private static final String[] HEADERS_TO_TRY = {
            "X-Forwarded-For",
            "Proxy-Client-IP",
            "WL-Proxy-Client-IP",
            "HTTP_X_FORWARDED_FOR",
            "HTTP_X_FORWARDED",
            "HTTP_X_CLUSTER_CLIENT_IP",
            "HTTP_CLIENT_IP",
            "HTTP_FORWARDED_FOR",
            "HTTP_FORWARDED",
            "HTTP_VIA",
            "REMOTE_ADDR" };

private String getClientIpAddress(HttpServletRequest request) {
    for (String header : HEADERS_TO_TRY) {
        String ip = request.getHeader(header);
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }
    }

    return request.getRemoteAddr();
}