Struts2 生成验证码

左手的ㄟ右手 2022-08-26 14:27 240阅读 0赞
  1. ImageUtil.java
  2. package com.ctl.util;
  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.ByteArrayInputStream;
  8. import java.io.ByteArrayOutputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import java.util.Random;
  14. import com.sun.image.codec.jpeg.JPEGCodec;
  15. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  16. public final class ImageUtil {
  17. private static final char[] chars = { '0', '1', '2', '3', '4', '5', '6',
  18. '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  19. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
  20. 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  21. 'k', 'x', 'y', 'z', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  22. 'u', 'v', 'w' };
  23. private static final int SIZE = 4;
  24. private static final int LINES = 6;
  25. private static final int WIDTH = 100;
  26. private static final int HEIGHT = 60;
  27. private static final int FONT_SIZE = 30;
  28. public static Map<String, BufferedImage> createImage() {
  29. StringBuffer sb = new StringBuffer();
  30. BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
  31. BufferedImage.TYPE_INT_RGB);
  32. Random ran = new Random();
  33. Graphics graphic = image.getGraphics();
  34. graphic.setColor(new Color(55 + ran.nextInt(200),
  35. 55 + ran.nextInt(200), 55 + ran.nextInt(200)));
  36. graphic.fillRect(0, 0, WIDTH, HEIGHT);
  37. // 画随机字符
  38. for (int i = 1; i <= SIZE; i++) {
  39. int r = ran.nextInt(chars.length);
  40. graphic.setColor(getRandomColor());
  41. graphic.setFont(new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE));
  42. graphic.drawString(chars[r] + "", (i - 1) * WIDTH / SIZE,
  43. HEIGHT / 2);
  44. sb.append(chars[r]);// 将字符保存,存入Session
  45. }
  46. // 画干扰线
  47. for (int i = 1; i <= LINES; i++) {
  48. graphic.setColor(getRandomColor());
  49. graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
  50. ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
  51. }
  52. Map<String, BufferedImage> map = new HashMap<String, BufferedImage>();
  53. map.put(sb.toString(), image);
  54. return map;
  55. }
  56. public static Color getRandomColor() {
  57. Random ran = new Random();
  58. Color color = new Color(ran.nextInt(256), ran.nextInt(256),
  59. ran.nextInt(256));
  60. return color;
  61. }
  62. public static InputStream getInputStream(BufferedImage image)
  63. throws IOException {
  64. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  65. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
  66. encoder.encode(image);
  67. byte[] imageBts = bos.toByteArray();
  68. InputStream in = new ByteArrayInputStream(imageBts);
  69. return in;
  70. }
  71. }
  72. ImageAction.java
  73. package com.ctl.util;
  74. import java.awt.image.BufferedImage;
  75. import java.io.IOException;
  76. import java.io.InputStream;
  77. import java.util.Map;
  78. import javax.servlet.http.Cookie;
  79. /**
  80. * 显示验证码的处理
  81. * @author tarena
  82. *
  83. */
  84. public class ImageAction extends BaseAction{
  85. //input 无
  86. //output
  87. //stream result只能输出inputStream类型的属性值
  88. private InputStream imageStream;
  89. public String execute() throws IOException{
  90. //生成一个验证码图片
  91. Map<String,BufferedImage> map =
  92. ImageUtil.createImage();
  93. String code =
  94. map.keySet().iterator().next();
  95. BufferedImage image = map.get(code);
  96. session.put("code", code);//用于验证
  97. System.out.println(code);
  98. response.addCookie(new Cookie("code", code));
  99. //把图片给imageStream赋值
  100. imageStream =
  101. ImageUtil.getInputStream(image);
  102. //调用stream result将imageStream输出
  103. return "success";
  104. }
  105. public InputStream getImageStream() {
  106. return imageStream;
  107. }
  108. public void setImageStream(InputStream imageStream) {
  109. this.imageStream = imageStream;
  110. }
  111. }
  112. struts.xml
  113. <struts>
  114. <constant name="struts.multipart.maxSize" value="1048576000" /><!--
  115. 限制上传的最大的字节数 -->
  116. <!-- 设置struts2 上传文件时 保存的临时目录 -->
  117. <constant name="struts.multipart.saveDir" value="C:\temp" /><!--
  118. 临时目录 -->
  119. <package name="all" namespace="/" extends="struts-default">
  120. <!-- 产生验证码 -->
  121. <action name="image" class="com.ctl.util.ImageAction">
  122. <result type="stream">
  123. <param name="inputName">imageStream</param>
  124. </result>
  125. </action>
  126. <action name="upload" class="com.action.FileUploadAction">
  127. <result name="success">upload.jsp</result>
  128. <result name="input">upload.jsp</result>
  129. </action>
  130. </package>
  131. </struts>
  132. html页面
  133. <tr>
  134. <td class="login-text02">验证码:</td>
  135. <td class="width70"><input id="yzm" type="text" name="yzmn"
  136. size="10" class="width70" value="" /></td>
  137. <td><img src="<%=request.getContextPath()%>/image"
  138. style="width:45px;height:21px; margin-left: -130px"
  139. id="imageCode" alt="验证码" title="点击更换" /></td>
  140. </tr>
  141. // 变换验证码
  142. $('#imageCode').click(function() {
  143. $('#imageCode').attr('src', "image?ts="+Math.random());
  144. });
  145. function getCookie(c_name)
  146. {
  147. if (document.cookie.length>0)
  148. {
  149. c_start=document.cookie.indexOf(c_name + "=")
  150. if (c_start!=-1)
  151. {
  152. c_start=c_start + c_name.length+1
  153. c_end=document.cookie.indexOf(";",c_start)
  154. if (c_end==-1) c_end=document.cookie.length
  155. return unescape(document.cookie.substring(c_start,c_end))
  156. }
  157. }
  158. return ""
  159. }
  160. function checkLogin() {
  161. var yzmv = document.frm.yzmn.value;
  162. if (yzmv == "") {
  163. alert("验证码不能为空");
  164. document.frm.yzmn.focus();
  165. return false;
  166. }
  167. var value=getCookie('code');
  168. //alert(value);
  169. if(yzmv!=value){
  170. alert("验证码错误");
  171. document.frm.yzmn.focus();
  172. return false;
  173. }
  174. }

发表评论

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

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

相关阅读

    相关 Struts2框架验证

    struts2有框架验证,我这边主要说的是XML配置的struts2的框架验证,继承validate方法的验证还是比较容易的,至于有人用注解来验证不怎么常见我也不讲了(感觉好多

    相关 验证生成

    视图函数里 > 1,导入图片,画布,画笔,画笔上的字体,定义坐标 > 2,从外部导入字体(字体导入到static静态中) > 3,随机生成验证码,验证码颜色,验证