不用想太复杂,就是两个比较String ip = request.getRemoteAddr(); if (ip.compareTo("192.168.0.1")>=0 && ip.compareTo("192.168.0.100")<=0 || ip.compareTo("192.168.1.1")>=0 && ip.compareTo("192.168.1.100")<=0) { //...... }
String ip = request.getRemoteAddr(); if (ip.compareTo("192.168.0.1")>=0 && ip.compareTo("192.168.0.100")<=0 || ip.compareTo("192.168.1.1")>=0 && ip.compareTo("192.168.1.100")<=0) { //...... }
这个判断明显是有问题的 Compares the specified String to this String using the Unicode values of the characters. Answer 0 if the strings contain the same characters in the same order. Answer a negative integer if the first non-equal character in this String has a Unicode value which is less than the Unicode value of the character at the same position in the specified string, or if this String is a prefix of the specified string. Answer a positive integer if the first non-equal character in this String has a Unicode value which is greater than the Unicode value of the character at the same position in the specified string, or if the specified String is a prefix of the this String.
这个不行 192.168.0.99 99比100大 返回的是false
String ip = "192.168.1.4";
String ips[] = ip.split("\\."); //192.168.0.1 String ad = ips[2];
然后就是判断了。最简单的就是把IP地址转换成整数(Long)了。
判断IP是否大于 192168000001 而且小于 192168000100
然后再判断跳转了。
public class IpFilter extends IpFilterAdapter{ @Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException { //这是一个例子 你自己看看 根据截取的内容 来设置你要过滤的IP
HttpServletRequest request1 = (HttpServletRequest)request;
HttpServletResponse response1 = (HttpServletResponse)response;
//获得客户端的IP地址
String ip = request.getRemoteAddr() + "";
System.out.println(ip);
//以转义字符\\对.进行转义 拆分 一般用,- | 用的比较多 用.就回出错 需要用转义字符转义
/*
ips[0] = 192
ips[1] = 168
ips[2] = 2
ips[3] = 47
*/
String ips[] = ip.split("\\.");
//192.168.0.1
int ad = Integer.parseInt(ips[3]);
/*
* 以上拆分方法还可以用以下代替:
* int ad = Integer.parseInt(ip.substring(ip.lastIndexOf(".") + 1));
*/
if(ad >= 1 && ad <= 50){
/*response.setContentType("text/html;charset=utf-8");
response.getWriter().print("您没有权限访问");*/
request1.getRequestDispatcher("error.jsp").forward(request1, response1);
}
else{ chain.doFilter(request, response);
}
}}
String ip1=ip.substring(0,ip.lastIndexOf("."));
String ip2=ip.substring(ip.lastIndexOf(".")+1,ip.length());判断ip1是否和192.168.0或192.168.1相等
如果相等再判断ip2是否小于100
if (ip.compareTo("192.168.0.1")>=0 && ip.compareTo("192.168.0.100")<=0 || ip.compareTo("192.168.1.1")>=0 && ip.compareTo("192.168.1.100")<=0) {
//......
}
String ip = request.getRemoteAddr();
if (ip.compareTo("192.168.0.1")>=0 && ip.compareTo("192.168.0.100")<=0
|| ip.compareTo("192.168.1.1")>=0 && ip.compareTo("192.168.1.100")<=0) {
//......
}
这个判断明显是有问题的
Compares the specified String to this String using the Unicode values of the
characters. Answer 0 if the strings contain the same characters in the same
order. Answer a negative integer if the first non-equal character in this String
has a Unicode value which is less than the Unicode value of the character at
the same position in the specified string, or if this String is a prefix of the
specified string. Answer a positive integer if the first non-equal character in
this String has a Unicode value which is greater than the Unicode value of the
character at the same position in the specified string, or if the specified
String is a prefix of the this String.
这个不行 192.168.0.99 99比100大 返回的是false
String ips[] = ip.split("\\.");
//192.168.0.1
String ad = ips[2];
String ad11 = ips[3]; if ((ad11.compareTo("100")>=0&&ad.compareTo("1")<=0)|| (ad.compareTo("1")<=0 &&ad11.compareTo("100")>=0) ){
System.out.println("yes");
}
试试这个把
Integer ii = Integer.valueOf(ip.replaceAll("\\.", ""));
if (ii.compareTo(19216801)>=0 && ii.compareTo(1921680100)<=0 || ii.compareTo(19216811)>=0 && ii.compareTo(1921681100)<=0) {
System.out.println("合格");
}else{
System.out.println("bu合格");
}
Integer ii = Integer.valueOf(ip.replaceAll("\\.", ""));
if ((ii.compareTo(1921680001)>=0 && ii.compareTo(1921680100)<=0) || (ii.compareTo(1921681001)>=0 && ii.compareTo(1921681100)<=0)) {
System.out.println("合格");
}else{
System.out.println("bu合格");
}
String ips[] = ip.split("\\.");
//192.168.0.1
String ad = ips[2];
String ad11 = ips[3];
if ((ad11.compareTo("100")<=0)&&ad.compareTo("1")<=0&& (ad.compareTo("1")<=0 &&ad11.compareTo("100")<=0) ){
System.out.println("yes");
}上面的符号判断错了 更正下下 测试后没问题了
必须用整数比较:
public static boolean check(String ip) {
try {
if (!ip.startsWith("192.168.0.") && !ip.startsWith("192.168.1.")) return false;
if (Integer.parseInt(ip.substring(10))<1 || Integer.parseInt(ip.substring(10))>100) return false;
} catch (Exception e) {return false;}
return true;
}
192.168.0.1 => 192*255*255*255+168*255*255+0*255+1
192.168.0.100 => 192*255*255*255+168*255*255+0*255+100