代码:

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

页面上调用:

请输入验证码:

 
   
  1. <input type="text">  
  2. <img src="AuthImg" id="authImg" οnclick="this.src='AuthImg?now='+new Date()" alt="点击刷新" >