java java中如何从请求中获取客户端的浏览器ip?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15082375/
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-10-31 18:25:24  来源:igfitidea点击:

How to get the client's browser ip from the request in java?

javajakarta-ee

提问by Arun

I know it is quite easy to get the client's IP and server IP from the httprequest in java. Is it possible to get the browser IP of the client at any means.

我知道从 java 中的 httprequest 获取客户端的 IP 和服务器 IP 非常容易。是否可以通过任何方式获取客户端的浏览器IP。

Thanks in advance

提前致谢

回答by user1111527

As I understand correctly

据我了解

You can get client IP with method

您可以使用方法获取客户端 IP

HttpServletRequest httpRequest = (HttpServletRequest) request;
String userIpAddress = httpRequest.getHeader("X-Forwarded-For");

Full quote: How to determine by what IP Address my website has been accessed?

完整报价: 如何确定我的网站被访问的 IP 地址?

Getting the server IP from request:

从请求中获取服务器IP

HttpServletRequest.getLocalAddr();

Full quote: Getting server address and application name

完整引用:获取服务器地址和应用程序名称

And getting the server ips can be done so:

获取服务器 ips 可以这样做

Inet4Address.getLocalHost().getHostAddress()

Full quote: Getting the IP address of the current machine using Java

全文引用: 使用Java获取当前机器的IP地址