`

Servlet生成彩色验证码

    博客分类:
  • java
阅读更多

 

很早以前用的,为了防止忘记,做个记录

java 代码
  1. import java.io.IOException;   
  2. import javax.servlet.ServletException;   
  3. import javax.servlet.ServletOutputStream;   
  4.   
  5. import javax.servlet.http.HttpServlet;   
  6. import javax.servlet.http.HttpServletRequest;   
  7. import javax.servlet.http.HttpServletResponse;   
  8. import javax.servlet.http.HttpSession;   
  9.   
  10. import com.sun.image.codec.jpeg.JPEGCodec;   
  11. import com.sun.image.codec.jpeg.JPEGImageEncoder;   
  12.   
  13. import java.awt.*;   
  14. import java.awt.image.*;   
  15. import java.util.*;   
  16.   
  17. public class ImageServlet   
  18.     extends HttpServlet {   
  19.   
  20.   public void doGet(HttpServletRequest request, HttpServletResponse response) throws  
  21.       ServletException, IOException {   
  22.     response.reset();   
  23.     response.setContentType("image/jpeg");   
  24.     response.setHeader("Pragma""No-cache");   
  25.     response.setHeader("Cache-Control""no-cache");   
  26.     response.setDateHeader("Expires"0);   
  27.   
  28. //在内存中创建图象   
  29.     int width = 58, height = 18;   
  30.     BufferedImage image = new BufferedImage(width, height,   
  31.                                             BufferedImage.TYPE_INT_RGB);   
  32.   
  33. //获取图形上下文   
  34.     Graphics g = image.getGraphics();   
  35.   
  36. //生成随机类   
  37.     Random random = new Random();   
  38.   
  39. //设定背景色   
  40.     g.setColor(new Color(201 + random.nextInt(50), 201 + random.nextInt(50),   
  41.                          201 + random.nextInt(50)));   
  42.     g.fillRect(00, width, height);   
  43.   
  44. //设定字体   
  45.     g.setFont(new Font("Times new Roman", Font.PLAIN, 18));   
  46.   
  47. //画边框   
  48. //g.setColor(new Color());   
  49. //g.drawRect(0,0,width-1,height-1);   
  50.   
  51. //随机产生155条干扰线,使图象中的认证码不易被其它程序探测到   
  52.     g.setColor(new Color(160 + random.nextInt(41), 160 + random.nextInt(41),   
  53.                          160 + random.nextInt(41)));   
  54.     for (int i = 0; i < 155; i++) {   
  55.       int x = random.nextInt(width);   
  56.       int y = random.nextInt(height);   
  57.       int xl = random.nextInt(12);   
  58.       int yl = random.nextInt(12);   
  59.       g.drawLine(x, y, x + xl, y + yl);   
  60.     }   
  61.   
  62. //取随机产生的认证码(4位数字)   
  63.     String sRand = "";   
  64.     for (int i = 0; i < 4; i++) {   
  65.       String rand = String.valueOf(random.nextInt(10));   
  66.       sRand += rand;   
  67.   
  68. //将认证码显示到图象中   
  69.       g.setColor(new Color(20 + random.nextInt(111), 20 + random.nextInt(111),   
  70.                            20 + random.nextInt(111)));   
  71.   
  72. //调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成   
  73.       g.drawString(rand, 13 * i + 616);   
  74.     }   
  75.     HttpSession session = null;   
  76.   
  77.     session = request.getSession(true);   
  78.   
  79.     //将认证码存入SESSION   
  80.     session.setAttribute("rand", sRand);   
  81.   
  82. //图象生效   
  83.     g.dispose();   
  84.   
  85. //输出图象到页面   
  86. //ImageIO.write(image, "JPEG", resp.getOutputStream());   
  87. //response.setContentType("image/jpeg");   
  88.     ServletOutputStream out = response.getOutputStream();   
  89.     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
  90.     encoder.encode(image);   
  91.     out.close();   
  92.   }   
  93. }   
  94.   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics