Java写一下获取网页请求时的IP地址代码
时间: 2023-03-25 21:00:29 浏览: 91
以下是Java获取网页请求时的IP地址代码:
```
import javax.servlet.http.HttpServletRequest;
public class GetIPAddress {
public static String getIPAddress(HttpServletRequest request) {
String ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
}
return ipAddress;
}
}
```
这段代码通过HttpServletRequest对象获取请求头中的IP地址信息,如果请求头中没有IP地址信息,则获取远程客户端的IP地址。
阅读全文