使用Servlet生成图片验证码

   3import java.awt.Color;  
   
4import java.awt.Font;  
   
5import java.awt.Graphics;  
   
6import java.awt.image.BufferedImage;  
   
7import java.io.IOException;  
   
8import java.util.Random;  
   
9.   
  
10import javax.imageio.ImageIO;  
  
11import javax.servlet.ServletException;  
  
12import javax.servlet.http.HttpServlet;  
  
13import javax.servlet.http.HttpServletRequest;  
  
14import javax.servlet.http.HttpServletResponse;  
  
15import javax.servlet.http.HttpSession;  
  
16.   
  
17/** 
  18.  * 用于生存图形验证码的Servlet 
  19.  * 
@author ChenGuanwei 
  20.  * 
  21.  
*/  
  
22public class AuthImg extends HttpServlet {  
  
23.   
  
24.     //设置图形验证码中字符串的字体和大小  
  25.     private Font mFont = new Font("Arial Black",Font.PLAIN,16);  
  
26.   
  
27.     @Override  
  
28.     public void init() throws ServletException {  
  
29.         super.init();  
  
30.     }  
  
31.       
  
32.     @Override  
  
33.     protected void service(HttpServletRequest request, HttpServletResponse response)  
  
34.             throws ServletException, IOException {  
  
35.           
  
36.         //阻止生成的页面内容被缓存,保证每次重新生成验证码  
  37.         response.setHeader("Pragma","No-cache");  
  
38.         response.setHeader("Cache-Control","no-cache");  
  
39.         response.setDateHeader("Expires",0);  
  
40.         response.setContentType("image/jpeg");  
  
41.           
  
42.         //指定验证码图片的大小  
  43.         int width = 100;  
  
44.         int height = 18;  
  
45.           
  
46.         //生成一张新图片  
  47.         BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);  
  
48.         //在图片中绘制内容  
  49.         Graphics g = image.getGraphics();  
  
50.         Random random  = new Random();  
  
51.         g.setColor(getRandColor(200,250));  
  
52.         g.fillRect(1,1,width-1,height-1);  
  
53.         g.setColor(new Color(102,102,102));  
  
54.         g.drawRect(0,0, width-1, height-1);  
  
55.         g.setFont(mFont);  
  
56.           
  
57.         //随即生成线条,让图片看起来更加杂乱  
  58.         g.setColor(getRandColor(160,200));  
  
59.         for(int i=0;i<155;i++) {  
  
60.             int x = random.nextInt(width-1);  
  
61.             int y = random.nextInt(height-1);  
  
62.             int x1 = random.nextInt(6+ 1;  
  
63.             int y1 =  random.nextInt(12+ 1;  
  
64.             g.drawLine(x,y,x+x1,y+y1);  
  
65.         }  
  
66.           
  
67.         for(int i=0;i<70;i++) {  
  
68.             int x = random.nextInt(width-1);  
  
69.             int y = random.nextInt(height-1);  
  
70.             int x1 = random.nextInt(12+ 1;  
  
71.             int y1 =  random.nextInt(6+ 1;  
  
72.             g.drawLine(x,y,x-x1,y-y1);  
  
73.         }  
  
74.           
  
75.         //该变量用于保存系统生成的随即字符  
  76.         String sRand = "";  
  
77.         for(int i=0;i<6;i++) {  
  
78.             //取得一个随即字符  
  79.             String tmp = getRandChar();  
  
80.             sRand += tmp;  
  
81.             //将系统生成的随即字符添加到图形验证码图片上  
  82.             g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));  
  
83.             g.drawString(tmp,15*i+10,15);  
  
84.         }  
  
85.           
  
86.         //取得用户session  
  87.         HttpSession session = request.getSession();  
  
88.         session.setAttribute("auth",sRand);  
  
89.         g.dispose();  
  
90.           
  
91.         //输出图形验证码  
  92.         ImageIO.write(image,"JPEG",response.getOutputStream());  
  
93.     }  
  
94.   
  
95.     //生成随即颜色  
  96.     private Color getRandColor(int fc,int bc) {  
  
97.         Random random = new Random();  
  
98.         if(fc>255)  
  
99.             fc = 255;  
 
100.         if(bc>255)  
 
101.             bc = 255;  
 
102.         int r = fc + random.nextInt(bc-fc);  
 
103.         int g = fc + random.nextInt(bc-fc);  
 
104.         int b = fc + random.nextInt(bc-fc);  
 
105.         return new Color(r,g,b);  
 
106.     }  
 
107.       
 
108.     //生成随即字符  
 109.     private String getRandChar() {  
 
110.         int rand = (int)Math.round(Math.random()*2);  
 
111.         long itmp = 0;  
 
112.         char ctmp = '\u0000';  
 
113.         //根据rand的值来决定是生成一个大写字母、小写字母还是数字  
 114.         switch(rand) {  
 
115.             //生成大写字母  
 116.             case 1:  
 
117.                 itmp = Math.round(Math.random()*25+65);  
 
118.                 ctmp = (char)itmp;  
 
119.                 return String.valueOf(ctmp);  
 
120.             //生成小写字母  
 121.             case 2:  
 
122.                 itmp = Math.round(Math.random()*25+97);  
 
123.                 ctmp = (char)itmp;  
 
124.                 return String.valueOf(ctmp);  
 
125.             //生成数字  
 126.             default:  
 
127.                 itmp = Math.round(Math.random()*9);  
 
128.                 ctmp = (char)itmp;  
 
129.                 return String.valueOf(itmp);  
 
130.         }  
 
131.     }  
 
132.   
 
133. } 
posted on 2009-06-05 10:05  HEYUTAO  阅读(367)  评论(0编辑  收藏  举报