判断用户提交的是否为IP地址,看似简单,貌似就是字符串什么的对比,却也用了不少技术

我是用struts的validate判断的,只把validate方法粘贴上

public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request,HttpServletResponse response) {
  ActionErrors errors = new ActionErrors() ;
   //IP
   if(kk_BlacklistVehicle_UserEnrol_IP==null||kk_BlacklistVehicle_UserEnrol_IP.equals(""))
   {
    errors.add("errorKK_BlacklistVehicle_UserEnrol",new ActionMessage("kk_BlacklistVehicle_UserEnrol_IP.null")) ;
   }
   else
   {
    if(kk_BlacklistVehicle_UserEnrol_IP.length()>15)
    {
     errors.add("errorKK_BlacklistVehicle_UserEnrol",new ActionMessage("kk_BlacklistVehicle_UserEnrol_IP.leng")) ;
    }
    else
    {
     StringTokenizer st = new StringTokenizer(kk_BlacklistVehicle_UserEnrol_IP,".",false);//去掉IP地址中的'.'
     System.out.println( "Token Total: " + st.countTokens() );
     boolean b = true;
     while( st.hasMoreElements() )
     {
      for (int i = st.nextToken().length();--i>=0;)
      {
       //判断去掉'.'后其他字符是否都是数字
       if (!(kk_BlacklistVehicle_UserEnrol_IP.charAt(i) >= 48 && kk_BlacklistVehicle_UserEnrol_IP.charAt(i) <= 57)) 
       {
        System.out.println("您输入的IP中除了数字和.还包括了其他字符!请重新输入.");
        errors.add("errorKK_BlacklistVehicle_UserEnrol",new ActionMessage("kk_BlacklistVehicle_UserEnrol_IP.ZiFu")) ;
        b = false; 
        break;
       } 
       else 
       { 
        b = true;
       }
      }
     }
     boolean isIP = true;
     if(b)
     {
      //去掉'.'后其他字符是否都是数字进入到此if
      int count = 0;
      String temp = kk_BlacklistVehicle_UserEnrol_IP;
      if(temp.charAt(0) == '.' || temp.charAt(temp.length()-1) == '.' || temp.length()>15)
      {
       //判断第一位和最后一位是不是'.',是的话isIp设置为false
       isIP = false;
      }
      //这个for循环判断'.'是不是3个,如果不是3个,设置isIP为fasle
      for(int i=0;i<temp.length();i++)
      {
       if(temp.charAt(i) == '.')
       {
        count++;
       }
      }
       if(count != 3)
       {
        isIP = false;
       }
     }
     if(!isIP)
     {
      //isIP为false,提示错误信息
      errors.add("errorKK_BlacklistVehicle_UserEnrol",new ActionMessage("kk_BlacklistVehicle_UserEnrol_IP.GeShi")) ;
     }
    }
   }
  return  errors;
 }

 PS:kk_BlacklistVehicle_UserEnrol_IP为用户提交上来的property
其他的我都加了详细的注释

粘贴个经我修改过的判断IP的带有main方法的类

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class ValidIP
{
 public static boolean isNumeric(String str)
 {
  StringTokenizer st = new StringTokenizer(str,".",false);
  System.out.println( "Token Total: " + st.countTokens() );
  boolean b = true;
  while( st.hasMoreElements() )
  {
   for (int i = st.nextToken().length();--i>=0;)
   {
    if (!(str.charAt(i) >= 48 && str.charAt(i) <= 57)) 
    {
     System.out.println("您输入的不是纯数字!请重新输入."); 
     b = false; 
     continue;
    } 
    else 
    { 
     b = true; 
    }
   }
  }
  return b;
 }

 public boolean validIP(String ip)
 {
  int count = 0;
  boolean isIP = true;
  String temp = ip;
  if(temp.charAt(0) == '.' || temp.charAt(temp.length()-1) == '.' || temp.length()>15)
  {
   isIP = false;
  }
  for(int i=0;i<temp.length();i++)
  {
   if(temp.charAt(i) == '.')
   {
    count++;
   }
  }
   if(count != 3)
   {
    isIP = false;
   }
  return isIP;
 }
 public static void main(String[] args) throws IOException
 {
//  System.out.print("Input IP Address:");
//  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//  String tempbr = new String(br.readLine());
//此处3行注释打开是由自己输入字符,我为了方便就把这里注释掉直接自己传字符串了
  ValidIP isip = new ValidIP();
  boolean a = isip.isNumeric("111.111.1.1.21");//判断传进去的字符串是否除了'.'就是数字
  System.out.println(a);
  if(a)//是的话进入这个if
  {
   boolean bl = isip.validIP("111.111.1.1.21");//判断这个ip地址的规格对不对
   System.out.println(bl);//对打印true错false
  }
 }
}

 下面再粘个用swing包的判断IP的类,有main方法,但我未修改,你自己修改一下吧,好象是这个输入英文字母也判断为正确的IP地址,用的话参考我上边的例子,用StringTokenizer控制一下,如果你有更好的方法,请留言,也让我学习学习

package test;

import javax.swing.JOptionPane;

public class IDTests
{
 public static void main(String as[])
 {
  String id = new String ();
  id=JOptionPane.showInputDialog(null,"Please input ID:");
  int add=id.length();
  String ad = new String();
  ad=String.valueOf(add);
  String test = new String();
  boolean b1=false;
  boolean b2=false;
  boolean b3=false;
  boolean b4=false;
 
  int found=1;
  if(id.charAt(0) == '.' || id.charAt(id.length()-1) == '.'|| add >15)
  {
   JOptionPane.showMessageDialog(null,"IPAddress is error");
  }
  else
  {
   for(int i=0;i<add;i++ )
   {
    if(String.valueOf(id.charAt(i)).equals("."))
    {
     //JOptionPane.showMessageDialog(null,String.valueOf(i));
     int testadd = Integer.parseInt(test);
     if(found==1)
     {
      if(testadd > 0 && testadd< 255)
      {
       b1=true;
      }
     }
     if(found==2)
     {
      if(testadd >=0 && testadd< 255)
      {
       b2=true;
      }
     }
     if(found==3)
     {
      if(testadd >=0 && testadd < 255)
      {
       b3=true;
      }
     }
     test=new String();
     found++;
    }
    else
    {
     test=test+String.valueOf(id.charAt(i));
     if(i==(add-1))
     {
      int testadd = Integer.parseInt(test);
      if(testadd >=0 && testadd< 255)
      {
       b4=true;
      }
  
     }
    }
   }
  }
  //JOptionPane.showMessageDialog(null,"b1+"+String.valueOf(b1));
  //JOptionPane.showMessageDialog(null,"b2"+String.valueOf(b2));
  //JOptionPane.showMessageDialog(null,"b3"+String.valueOf(b3));
  //JOptionPane.showMessageDialog(null,"b4"+String.valueOf(b4));
  if(b1&&b2&&b3&&b4)
  {
   JOptionPane.showMessageDialog(null,"IPAddress is exactly!");
  }
  else
  {
   JOptionPane.showMessageDialog(null,"IPAddress is error");
  }
 }
}