一、servlet实现验证码的生成:
第一步:
web.xml配置
01.<servlet>
02. <servlet-name>image</servlet-name>
03. <servlet-class>org.test.web.AuthImage</servlet-class>
04.</servlet>
05.
06.<servlet-mapping>
07. <servlet-name>image</servlet-name>
08. <url-pattern>/authImage</url-pattern>
09.</servlet-mapping>
第二步:
servlet配置:
01.public class AuthImage extends HttpServlet
02.{
03.
04. private static final String CONTENT_TYPE = "text/html; charset=gb2312";
05. //设置字母的大小,大小
06. private Font mFont = new Font("Times New Roman", Font.PLAIN, 17);
07. public void init() throws ServletException
08. {
09. super.init();
10. }
11. Color getRandColor(int fc,int bc)
12. {
13. Random random = new Random();
14. if(fc>255) fc=255;
15. if(bc>255) bc=255;
16. int r=fc+random.nextInt(bc-fc);
17. int g=fc+random.nextInt(bc-fc);
18. int b=fc+random.nextInt(bc-fc);
19. return new Color(r,g,b);
20. }
21.
22. public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
23. {
24. response.setHeader("Pragma","No-cache");
25. response.setHeader("Cache-Control","no-cache");
26. response.setDateHeader("Expires", 0);
27. //表明生成的响应是图片
28. response.setContentType("image/jpeg");
29.
30. int width=100, height=18;
31. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
32.
33. Graphics g = image.getGraphics();
34. Random random = new Random();
35. g.setColor(getRandColor(200,250));
36. g.fillRect(1, 1, width-1, height-1);
37. g.setColor(new Color(102,102,102));
38. g.drawRect(0, 0, width-1, height-1);
39. g.setFont(mFont);
40.
41. g.setColor(getRandColor(160,200));
42.
43. //画随机线
44. for (int i=0;i<155;i++)
45. {
46. int x = random.nextInt(width - 1);
47. int y = random.nextInt(height - 1);
48. int xl = random.nextInt(6) + 1;
49. int yl = random.nextInt(12) + 1;
50. g.drawLine(x,y,x + xl,y + yl);
51. }
52.
53. //从另一方向画随机线
54. for (int i = 0;i < 70;i++)
55. {
56. int x = random.nextInt(width - 1);
57. int y = random.nextInt(height - 1);
58. int xl = random.nextInt(12) + 1;
59. int yl = random.nextInt(6) + 1;
60. g.drawLine(x,y,x - xl,y - yl);
61. }
62.
63. //生成随机数,并将随机数字转换为字母
64. String sRand="";
65. for (int i=0;i<6;i++)
66. {
67. int itmp = random.nextInt(26) + 65;
68. char ctmp = (char)itmp;
69. sRand += String.valueOf(ctmp);
70. g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
71. g.drawString(String.valueOf(ctmp),15*i+10,16);
72. }
73.
74. HttpSession session = request.getSession(true);
75. session.setAttribute("rand",sRand);
76. g.dispose();
77. ImageIO.write(image, "JPEG", response.getOutputStream());
78. }
79. public void destroy()
80. {
81. }
82.}
第三步:
页面访问:
01.<img src="authImage"/>
二、纯jsp实现验证码的生产
第一步:
jsp代码:
01.<%@ page language="java" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
02. contentType="image/jpeg" pageEncoding="UTF-8"%>
03.
04.
05.<% //设置页面不缓存
06. response.setHeader("Pragma","No-cache");
07. response.setHeader("Cahce-Control","no-cache");
08. response.setDateHeader("Expires",0);
09. //在内存中创建图片
10. int width=60,height=20;
11. BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
12. //获取图形上下文
13. Graphics g= image.getGraphics();
14. //生成随机类
15. Random random= new Random();
16. //设置背景颜色
17. g.setColor(new Color(160,200,100));
18. g.fillRect(0,0,width,height);
19. //设置字体
20. g.setFont(new Font("Times New Roman",Font.PLAIN,18));
21. //随机产生50条干扰线,使图形中的验证码不易被其他的程序探测到
22. g.setColor(new Color(160,200,200));
23. for(int i=0;i<50;i++)
24. {
25. int x=random.nextInt(width);
26. int y=random.nextInt(height);
27. int x1=random.nextInt(width);
28. int y1=random.nextInt(height);
29. g.drawLine(x,y,x+x1,y+y1);
30. }
31. //随机产生验证码(6位数字)
32. String sRand="";
33. for(int i=0;i<6;i++)
34. {
35. String rand=String.valueOf(random.nextInt(10));
36. sRand+=rand;
37. //将验证码显示到图象
38. g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
39. g.drawString(rand,13*i+6,16);
40. }
41. session.setAttribute("rand",sRand); //将产生的验证码存储到sesson中
42. g.dispose();
43. ImageIO.write(image,"JPEG",response.getOutputStream());
44. out.clear(); //***********
45. out=pageContext.pushBody();//**********
46. %>
第二步:
页面访问:
01.<img src="xxx.jsp"/>
三、struts实现验证码生成:
第一步:
工具类RandomNumUtil 代码:
01.package com.cn.hospital.util;
02.
03.import java.awt.Color;
04.import java.awt.Font;
05.import java.awt.Graphics;
06.import java.awt.image.BufferedImage;
07.import java.io.ByteArrayInputStream;
08.import java.io.ByteArrayOutputStream;
09.import java.util.Random;
10.import javax.imageio.ImageIO;
11.import javax.imageio.stream.ImageOutputStream;
12.
13.public class RandomNumUtil {
14.
15. private ByteArrayInputStream image;//图像
16. private String str;//验证码
17.
18. private RandomNumUtil(){
19. init();//初始化属性
20. }
21. /*
22. * 取得RandomNumUtil实例
23. */
24. public static RandomNumUtil Instance(){
25. return new RandomNumUtil();
26. }
27. /*
28. * 取得验证码图片
29. */
30. public ByteArrayInputStream getImage(){
31. return this.image;
32. }
33. /*
34. * 取得图片的验证码
35. */
36. public String getString(){
37. return this.str;
38. }
39.
40. private void init() {
41. // 在内存中创建图象
42. int width=85, height=20;
43. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
44. // 获取图形上下文
45. Graphics g = image.getGraphics();
46. // 生成随机类
47. Random random = new Random();
48. // 设定背景色
49. g.setColor(getRandColor(200,250));
50. g.fillRect(0, 0, width, height);
51. // 设定字体
52. g.setFont(new Font("Times New Roman",Font.PLAIN,18));
53. // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
54. g.setColor(getRandColor(160,200));
55. for (int i=0;i<155;i++)
56. {
57. int x = random.nextInt(width);
58. int y = random.nextInt(height);
59. int xl = random.nextInt(12);
60. int yl = random.nextInt(12);
61. g.drawLine(x,y,x+xl,y+yl);
62. }
63. // 取随机产生的认证码(6位数字)
64. String sRand="";
65. for (int i=0;i<6;i++){
66. String rand=String.valueOf(random.nextInt(10));
67. sRand+=rand;
68. // 将认证码显示到图象中
69. g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
70. // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
71. g.drawString(rand,13*i+6,16);
72. }
73. //赋值验证码
74. this.str=sRand;
75.
76. //图象生效
77. g.dispose();
78. ByteArrayInputStream input=null;
79. ByteArrayOutputStream output = new ByteArrayOutputStream();
80. try{
81. ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
82. ImageIO.write(image, "JPEG", imageOut);
83. imageOut.close();
84. input = new ByteArrayInputStream(output.toByteArray());
85. }catch(Exception e){
86. System.out.println("验证码图片产生出现错误:"+e.toString());
87. }
88.
89. this.image=input;/* 赋值图像 */
90. }
91. /*
92. * 给定范围获得随机颜色
93. */
94. private Color getRandColor(int fc,int bc){
95. Random random = new Random();
96. if(fc>255) fc=255;
97. if(bc>255) bc=255;
98. int r=fc+random.nextInt(bc-fc);
99. int g=fc+random.nextInt(bc-fc);
100. int b=fc+random.nextInt(bc-fc);
101. return new Color(r,g,b);
102. }
103.}
第二步:
验证码输出action
01.package com.cn.hospital.action;
02.
03.import java.io.ByteArrayInputStream;
04.import java.io.ByteArrayOutputStream;
05.
06.import org.springframework.context.annotation.Scope;
07.import org.springframework.stereotype.Controller;
08.
09.import com.cn.hospital.util.RandomCharUtil;
10.import com.cn.hospital.util.RandomNumUtil;
11.import com.opensymphony.xwork2.ActionContext;
12.import com.opensymphony.xwork2.ActionSupport;
13.
14.@Controller("utilAction")
15.@Scope("prototype")
16.public class UtilAction extends ActionSupport{
17.
18. private static final long serialVersionUID = -7193209177116825032L;
19. private ByteArrayInputStream inputStream;
20.
21. private int width;
22. private int height;
23. private int fontSize;
24. private int codeLength;
25. private int disturbType;
26.
27. public String validNumGenerate() throws Exception{
28. RandomNumUtil rdnu=RandomNumUtil.Instance();
29. this.setInputStream(rdnu.getImage());//取得带有随机字符串的图片
30. ActionContext.getContext().getSession().put("random", rdnu.getString());//取得随机字符串放入HttpSession
31. return SUCCESS;
32. }
33.
34.
35. public void setInputStream(ByteArrayInputStream inputStream) {
36. this.inputStream = inputStream;
37. }
38.
39. public ByteArrayInputStream getInputStream() {
40. return inputStream;
41. }
42.
43.
44.}
第三步:
struts.xml配置
01.<!-- 产生随机验证码 -->
02. <action name="randNum" class="utilAction" method="validNumGenerate">
03. <result name="success" type="stream">
04. <param name="contentType">image/jpeg</param>
05. <param name="inputName">inputStream</param>
06. </result>
07. </action>
第四步:
页面访问:
01.<img src="randNum"/>