生成jsp验证码的代码详解(servlet版)

package util;
import java.util.*;   
import java.io.*;   
import java.awt.*;   
import java.awt.image.*;   
import javax.servlet.*;   
import javax.servlet.http.*;   
import com.sun.image.codec.jpeg.*;   

/*
 * 生成验证码图片
 */
public class ValidateCode extends HttpServlet   
{  
 
      private Font imgFont=new Font("宋体",Font.BOLD,16);//设置字体   
    
       public void doPost(HttpServletRequest request,HttpServletResponse response)   
                                     throws ServletException,IOException   
       {   
               doGet(request,response);   
       }   
    

          //设置浏览器不要缓存此图片
          response.setHeader("Pragma","No-cache");
          response.setHeader("Cache-Control","no-cache");
          response.setDateHeader("Expires", 0);

  
       /* 
        * 在给定范围内获得随机颜色 
        */ 
        private Color getRandColor(int fc,int bc)
        { 
                Random random = new Random(); 
                if(fc>255) fc=255; 
                if(bc>255) bc=255; 
                int r=fc+random.nextInt(bc-fc); 
                int g=fc+random.nextInt(bc-fc); 
                int b=fc+random.nextInt(bc-fc); 
                return new Color(r,g,b); 
        }

        /////////////////////////////////////////////////////////////////////////
        public void doGet(HttpServletRequest request,HttpServletResponse response)     
                               throws ServletException,IOException   
        {   
                String strCode  ="";   //验证码(字符串形式)
                int intCode  =0;     //验证码(数字形式)
                Random random = new Random();//随机类
      
                //生成随机四位数字
                intCode = (new Random()).nextInt(9999);   
                if(intCode<1000)
                {
                        intCode+=1000;  
                }


                 //将数字转化为字符串
                 strCode=intCode+"";   
      
                 /*
                  * 绘图
                  */

                 
                 response.setContentType("image/gif");   
                 //获得servlet输出流
                 ServletOutputStream servletOutputStream =response.getOutputStream();  
                 //创建表示图像的对象(参数是:宽、高、图片类型)
                 BufferedImage image=new BufferedImage(60,25,BufferedImage.TYPE_INT_RGB); 
                //创建表示图形上下文的对象   
                Graphics graph=image.getGraphics();
      
                 //设置此图形上下文的颜色
                graph.setColor(Color.blue);   
                //设定矩形的横、纵坐标,及宽、高
                graph.fillRect(1,1,48,18);   
      
               //将此图形上下文的字体设置为指定字体(参数为Font类型)
                graph.setFont(imgFont);   
    
               //随机产生10条干扰线,使图象中的认证码不易被其它程序探测到 
                graph.setColor(getRandColor(160,200)); 
                for (int i=0;i<10;i++) 
              {           
                        int x = random.nextInt(85); 
                        int y = random.nextInt(20); 
                        int xl = random.nextInt(12); 
                        int yl = random.nextInt(12); 
                        //在此图形上下文的坐标系统中,使用当前颜色在点 (x,y) 和 (x1,y1) 之间画一条线
                        graph.drawLine(x,y,x+xl,y+yl); 
              } 
      
            //分解验证码字符串
              char c;   
            for(int i=0;i<4;i++)   
            {   
                    //返回验证码字符串指定索引处的char值(单个数字)
                    c=strCode.charAt(i);   
                    //使用此图形上下文的当前字体和颜色绘制由指定 string 给定的文本(x->文本的横坐标,y->文本的纵坐标)
                    graph.drawString(c+"",9*i+4,16);     
             }   
      
           //对servlet输出流进行JPEG格式编码
            JPEGImageEncoder imageEncoder=JPEGCodec.createJPEGEncoder(servletOutputStream);   
            imageEncoder.encode(image);   
      
              //将获得的验证码字符串存入session
              HttpSession  session=request.getSession(true);   
              session.removeAttribute("VerifyCode");   
              session.setAttribute("VerifyCode",strCode);   
     
            //关闭输出流
              servletOutputStream.close();   
    
       }   
    
}   

原文地址:https://www.cnblogs.com/xcxcxcxc/p/5541238.html