在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java 生成验证码

2016-12-20 13:16| 发布者: zhangjf| 查看: 452| 评论: 0

摘要: /** * */ package com.becom.dkyd.webapp.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.ut ...
  1. /**
  2. *
  3. */
  4. package com.becom.dkyd.webapp.util;
  5. import java.awt.Color;
  6. import java.awt.Font;
  7. import java.awt.Graphics2D;
  8. import java.awt.image.BufferedImage;
  9. import java.io.IOException;
  10. import java.util.Random;
  11. import javax.imageio.ImageIO;
  12. import javax.servlet.ServletException;
  13. import javax.servlet.ServletOutputStream;
  14. import javax.servlet.http.HttpServlet;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import javax.servlet.http.HttpSession;
  18. import com.becom.dkyd.common.Constant;
  19. public class RandomCodeServlet extends HttpServlet
  20. {
  21. /**
  22. * serialVersionUID
  23. */
  24. private static final long serialVersionUID = 5916019367240722024L;
  25. private int width = 60;
  26. private int height = 20;
  27. @Override
  28. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
  29. {
  30. BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  31. Graphics2D g = img.createGraphics();
  32. Random random = new Random();
  33. g.setColor(Color.WHITE);
  34. g.fillRect(0, 0, width, height);
  35. Font font = new Font("Times New Roman", Font.PLAIN, 18);
  36. g.setFont(font);
  37. // 边框
  38. g.setColor(Color.BLACK);
  39. g.drawRect(0, 0, width - 1, height - 1);
  40. // 干扰线
  41. g.setColor(Color.GRAY);
  42. for (int i = 0; i < 60; i++)
  43. {
  44. int x = random.nextInt(width);
  45. int y = random.nextInt(height);
  46. int x1 = random.nextInt(12);
  47. int y1 = random.nextInt(12);
  48. g.drawLine(x, y, x + x1, y + y1);
  49. }
  50. // 保存验证码
  51. StringBuffer randomCode = new StringBuffer();
  52. int red = 0;
  53. int green = 0;
  54. int blue = 0;
  55. // 生成验证码
  56. for (int i = 0; i < 4; i++)
  57. {
  58. String strRand = String.valueOf(random.nextInt(10));
  59. red = random.nextInt(110);
  60. green = random.nextInt(50);
  61. blue = random.nextInt(50);
  62. g.setColor(new Color(red, green, blue));
  63. g.drawString(strRand, 13 * i + 6, 16);
  64. randomCode.append(strRand);
  65. }
  66. // 验证码放入session中
  67. HttpSession session = req.getSession();
  68. //需要验证的地方
  69. //加入session
  70. // 禁止图象缓存
  71. resp.setHeader("Pragma", "no-cache");
  72. resp.setHeader("Cache-Control", "no-cache");
  73. resp.setDateHeader("Expires", 0);
  74. resp.setContentType("image/jpeg");
  75. // 输出图象
  76. ServletOutputStream sos = resp.getOutputStream();
  77. ImageIO.write(img, "jpeg", sos);
  78. sos.close();
  79. }
  80. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 11:26

Copyright 2015-2025 djqfx

返回顶部