猿问

代码如下,filter里的获取ip地址运行错误,帮忙看一下!

public class FiltIp implements Filter {
private FilterConfig filterConfig;
private int startIp;
private int endIp;
public void destroy() { 
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request =(HttpServletRequest)arg0;
HttpServletResponse response =(HttpServletResponse)arg1;
String strstartIp=filterConfig.getInitParameter("startIp");
String strendIp=filterConfig.getInitParameter("endIp");
request.setAttribute("strstartIp", strstartIp);
request.setAttribute("strendIp", strendIp);
startIp=Integer.parseInt(strstartIp.replace(".", ""));
endIp=Integer.parseInt(strendIp.replace(".", ""));
String reqIP= request.getRemoteAddr();
request.setAttribute("reqIP", reqIP);
reqIP=reqIP.replace(".", "");
int ip=Integer.parseInt(reqIP);
if(ip<startIp||ip>endIp){
request.getRequestDispatcher("/last/filtIp.jsp").forward(request, response);
}
System.out.println("request的过滤");
arg2.doFilter(arg0, arg1);
System.out.println("response的过滤"); 
}

public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
this.filterConfig=arg0;
}

}

慕斯709654
浏览 110回答 1
1回答

精慕HU

由于 0:0:0:0:0:0:0:1 是ipv6 的地址, 所以, 下面代码会转换错误。int ip=Integer.parseInt(reqIP);&nbsp;&nbsp;至于如何转换, 和 startIp, endIp格式有关。
随时随地看视频慕课网APP

相关分类

Java
我要回答