Struts 验证图片。

Java代码

1. package cn.com.lough.struts.action;
2.
3. import java.awt.Color;
4. import java.awt.Font;
5. import java.awt.Graphics;
6. import java.awt.image.BufferedImage;
7. import java.util.Random;
8.
9. import javax.imageio.ImageIO;
10. import javax.servlet.ServletOutputStream;
11. import javax.servlet.http.HttpServletRequest;
12. import javax.servlet.http.HttpServletResponse;
13. import javax.servlet.http.HttpSession;
14.
15. import org.apache.struts.action.Action;
16. import org.apache.struts.action.ActionForm;
17. import org.apache.struts.action.ActionForward;
18. import org.apache.struts.action.ActionMapping;
19. import org.apache.commons.lang.RandomStringUtils;
20.
21. /**
22. * MyEclipse Struts Creation date: 01-11-2007
23. *
24. * XDoclet definition:
25. *
26. * @struts.action validate="true"
27. */
28. public class ValidatecodeAction extends Action {
29. /*
30. * Generated Methods
31. */
32.
33. /**
34. * Method execute
35. *
36. * @param mapping
37. * @param form
38. * @param request
39. * @param response
40. * @return ActionForward
41. */
42. public ActionForward execute(ActionMapping mapping, ActionForm form,
43. HttpServletRequest request, HttpServletResponse response) {
44. try {
45. int width = 50;
46. int height = 18;
47. // 取得一个4位随机字母数字字符串
48. String s = RandomStringUtils.random(4, true, true);
49.
50. // 保存入session,用于与用户的输入进行比较.
51. // 注意比较完之后清除session.
52. HttpSession session = request.getSession(true);
53. session.setAttribute("validateCode", s);
54.
55. response.setContentType("images/jpeg");
56. response.setHeader("Pragma", "No-cache");
57. response.setHeader("Cache-Control", "no-cache");
58. response.setDateHeader("Expires", 0);
59.
60. ServletOutputStream out = response.getOutputStream();
61. BufferedImage image = new BufferedImage(width, height,
62. BufferedImage.TYPE_INT_RGB);
63. Graphics g = image.getGraphics();
64. // 设定背景色
65. g.setColor(getRandColor(200, 250));
66. g.fillRect(0, 0, width, height);
67.
68. // 设定字体
69. Font mFont = new Font("Times New Roman", Font.BOLD, 18);// 设置字体
70. g.setFont(mFont);
71.
72. // 画边框
73. // g.setColor(Color.BLACK);
74. // g.drawRect(0, 0, width - 1, height - 1);
75.
76. // 随机产生干扰线,使图象中的认证码不易被其它程序探测到
77. g.setColor(getRandColor(160, 200));
78. // 生成随机类
79. Random random = new Random();
80. for (int i = 0; i < 155; i++) {
81. int x2 = random.nextInt(width);
82. int y2 = random.nextInt(height);
83. int x3 = random.nextInt(12);
84. int y3 = random.nextInt(12);
85. g.drawLine(x2, y2, x2 + x3, y2 + y3);
86. }
87.
88. // 将认证码显示到图象中
89. g.setColor(new Color(20 + random.nextInt(110), 20 + random
90. .nextInt(110), 20 + random.nextInt(110)));
91.
92. g.drawString(s, 2, 16);
93.
94. // 图象生效
95. g.dispose();
96. // 输出图象到页面
97. ImageIO.write((BufferedImage) image, "JPEG", out);
98. out.close();
99. } catch (Exception e) {
100. e.printStackTrace();
101. }
102. return null;
103. }
104.
105. private Color getRandColor(int fc, int bc) { // 给定范围获得随机颜色
106. Random random = new Random();
107. if (fc > 255)
108. fc = 255;
109. if (bc > 255)
110. bc = 255;
111. int r = fc + random.nextInt(bc - fc);
112. int g = fc + random.nextInt(bc - fc);
113. int b = fc + random.nextInt(bc - fc);
114. return new Color(r, g, b);
115. }
116. }
117.
118.
119. jsp页面的调用
120. <html:img page="/validatecode.do" border="0" οnclick="this.src='/validatecode.do'" alt="请输入此验证码,如看不清请点击刷新。" style="cursor:pointer" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值