验证码

 1 package cn.meeting.utils;
 2 
 3 import java.awt.Color;
 4 import java.awt.Font;
 5 import java.awt.Graphics;
 6 import java.awt.image.BufferedImage;
 7 import java.io.IOException;
 8 import java.io.OutputStream;
 9 import java.util.Random;
10 
11 import javax.imageio.ImageIO;
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import javax.servlet.http.HttpSession;
17 
18 public class ImgServlet extends HttpServlet {
19     private static final long serialVersionUID = 1L;
20 
21     protected void doGet(HttpServletRequest request, HttpServletResponse response)
22             throws ServletException, IOException {
23         // 1:声明高宽
24         int width = 60;
25         int height = 30;
26         // 2:定义内存中的图片
27         BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
28         // 4:获取G
29         Graphics g = img.getGraphics();
30         // 5:设置背景为white
31         g.setColor(Color.WHITE);
32         g.fillRect(0, 0, width, height);
33         g.setFont(new Font("宋体", Font.BOLD, 18));
34         //
35         Random r = new Random();
36         StringBuilder stringBuilder = new StringBuilder();
37         String str="";
38         for (int i = 0; i < 4; i++) {
39             //int a = r.nextInt(10);   生成数字验证码
40             
41             //生成字母验证码
42              /*int t = r.nextInt(2);
43              char a = t ==0? (char)(r.nextInt(26)+'a'):(char)(r.nextInt(26)+'A');
44              stringBuilder.append(a);*/
45             
46             str = getRandomChar();
47             stringBuilder.append(str);
48             g.setColor(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
49             g.drawString("" + str, i * 15, 10 + r.nextInt(20));
50         }
51 
52         //------将验证码保存到session中去
53         
54         HttpSession session = request.getSession();
55         session.setAttribute("randomNum", str);
56         
57         for (int i = 0; i < 8; i++) {
58             g.setColor(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
59             g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
60         }
61 
62         //
63         g.dispose();
64 
65         // 设置输出的类型
66         response.setContentType("image/jpg");
67         OutputStream out = response.getOutputStream();
68         // 使用ImageIO
69         ImageIO.write(img, "JPEG", out);
70     }
71     
72     /*
73      * 生成验证码的方法
74      */
75      public static String getRandomChar() { 
76          int index = (int) Math.round(Math.random() * 2); 
77          String randChar = ""; 
78          switch (index) { 
79          case 0://大写字符 
80              randChar = String.valueOf((char)Math.round(Math.random() * 25 + 65)); 
81              break; 
82          case 1://小写字符 
83              randChar = String.valueOf((char)Math.round(Math.random() * 25 + 97)); 
84              break; 
85          default://数字 
86              randChar = String.valueOf(Math.round(Math.random() * 9)); 
87              break; 
88          } 
89          return randChar; 
90      } 
91 }
原文地址:https://www.cnblogs.com/fujilong/p/5327858.html