首页 热点资讯 义务教育 高等教育 出国留学 考研考公

java里,怎么取得IP地址

发布网友 发布时间:2022-04-24 17:34

我来回答

1个回答

热心网友 时间:2023-10-26 20:28

package A;
import javax.servlet.http.HttpServletRequest;
public class a {
public String getRemortIP(HttpServletRequest request)
{
if (request.getHeader("x-forwarded-for") == null)
{
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
接着有些了个jsp来调用:
<%@ page language="java" import="java.util.*,A.*" pageEncoding="utf-8"%>
<jsp:useBean id="test" class="A.a" scope="request" > </jsp:useBean>
<%
out.println("request.getRemoteAddr(): " + request.getRemoteAddr()); //这里输出的不是真正的ip
test.getIpAddr(); //这里的参数怎么写?
%>

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com