java生成随机验证码

桃扇骨 2022-01-31 03:13 514阅读 0赞
  1. package com;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.image.BufferedImage;
  6. import java.io.IOException;
  7. import java.util.Random;
  8. import javax.imageio.ImageIO;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.ServletOutputStream;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import javax.servlet.http.HttpSession;
  15. public class createimage extends HttpServlet {
  16. private static final long serialVersionUID = 1L;
  17. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  18. throws ServletException, IOException {
  19. response.setContentType("image/jpeg");
  20. response.setHeader("Pragma","No-cache");
  21. response.setHeader("Cache-Control","no-cache");
  22. response.setDateHeader("Expires", 0);
  23. HttpSession session=request.getSession();
  24. // 在内存中创建图象
  25. int width=60, height=20;
  26. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  27. // 获取图形上下文
  28. Graphics g = image.getGraphics();
  29. //生成随机类
  30. Random random = new Random();
  31. // 设定背景色
  32. g.setColor(getRandColor(200,250));
  33. g.fillRect(0, 0, width, height);
  34. //设定字体
  35. g.setFont(new Font("Times New Roman",Font.LAYOUT_RIGHT_TO_LEFT,18));
  36. //画边框
  37. //g.setColor(new Color());
  38. //g.drawRect(0,0,width-1,height-1);
  39. // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
  40. g.setColor(getRandColor(160,200));
  41. for (int i=0;i<155;i++) {
  42. int x = random.nextInt(width);
  43. int y = random.nextInt(height);
  44. int xl = random.nextInt(12);
  45. int yl = random.nextInt(12);
  46. g.drawLine(x,y,x+xl,y+yl);
  47. }
  48. // 取随机产生的认证码(4位数字)
  49. String sRand="";
  50. //randChar="01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  51. String randChar="A01h234fjBdCnDEFG67f8oHIJabcKLMNOijklmPQRSTrsUVtWXpqYZ590";
  52. for (int i=0;i<4;i++){
  53. String rand=String.valueOf(randChar.charAt(random.nextInt(10)));
  54. //String rand=String.valueOf(random.nextInt(10));
  55. sRand+=rand;
  56. // 将认证码显示到图象中
  57. // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  58. g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
  59. g.drawString(rand,13*i+6,16);
  60. }
  61. // 将认证码存入SESSION
  62. /* session.setAttribute(SessionKey._SESSION_CODE_,sRand.toLowerCase());*/
  63. // 图象生效
  64. g.dispose();
  65. ServletOutputStream responseOutputStream =response.getOutputStream();
  66. // 输出图象到页面
  67. ImageIO.write(image, "JPEG", responseOutputStream);
  68. //以下关闭输入流!
  69. responseOutputStream.flush();
  70. responseOutputStream.close();
  71. }
  72. Color getRandColor(int fc,int bc){//给定范围获得随机颜色
  73. Random random = new Random();
  74. if(fc>255) fc=255;
  75. if(bc>255) bc=255;
  76. int r=fc+random.nextInt(bc-fc);
  77. int g=fc+random.nextInt(bc-fc);
  78. int b=fc+random.nextInt(bc-fc);
  79. return new Color(r,g,b);
  80. }
  81. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  82. throws ServletException, IOException {
  83. processRequest(request, response);
  84. }
  85. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  86. throws ServletException, IOException {
  87. processRequest(request, response);
  88. }
  89. }

浏览器访问

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xldHRlcnNz_size_16_color_FFFFFF_t_70

发表评论

表情:
评论列表 (有 0 条评论,514人围观)

还没有评论,来说两句吧...

相关阅读