Struts 2 生成验证码
每篇一囧:有些事情,不是因为难以做到而失去信心,而是因为失去信心才变得难以做到。
0.用Struts 2 生成验证码
====================== 华丽丽的分割线 ======================
0.用Struts 2 生成验证码
第一步,定义一个生成验证码的工具类:
1 import java.awt.Color;
2 import java.awt.Font;
3 import java.awt.Graphics;
4 import java.awt.image.BufferedImage;
5 import java.io.ByteArrayInputStream;
6 import java.io.ByteArrayOutputStream;
7 import java.util.Random;
8 import javax.imageio.ImageIO;
9 import javax.imageio.stream.ImageOutputStream;
10
11 public class RandomNumUtil {
12
13 private ByteArrayInputStream image;//图像
14 private String str;//验证码
15
16 private RandomNumUtil(){
17 init();//初始化属性
18 }
19 /*
20 * 取得RandomNumUtil实例
21 */
22 public static RandomNumUtil Instance(){
23 return new RandomNumUtil();
24 }
25 /*
26 * 取得验证码图片
27 */
28 public ByteArrayInputStream getImage(){
29 return this.image;
30 }
31 /*
32 * 取得图片的验证码
33 */
34 public String getString(){
35 return this.str;
36 }
37
38 private void init() {
39 // 在内存中创建图象
40 int width=85, height=20;
41 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
42 // 获取图形上下文
43 Graphics g = image.getGraphics();
44 // 生成随机类
45 Random random = new Random();
46 // 设定背景色
47 g.setColor(getRandColor(200,250));
48 g.fillRect(0, 0, width, height);
49 // 设定字体
50 g.setFont(new Font("Times New Roman",Font.PLAIN,18));
51 // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
52 g.setColor(getRandColor(160,200));
53 for (int i=0;i<155;i++) {
54 int x = random.nextInt(width);
55 int y = random.nextInt(height);
56 int xl = random.nextInt(12);
57 int yl = random.nextInt(12);
58 g.drawLine(x,y,x+xl,y+yl);
59 }
60 // 取随机产生的认证码(6位数字)
61 String sRand="";
62 for (int i=0;i<6;i++){
63 String rand=String.valueOf(random.nextInt(10));
64 sRand+=rand;
65 // 将认证码显示到图象中
66 g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
67 // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
68 g.drawString(rand,13*i+6,16);
69 }
70 //赋值验证码
71 this.str=sRand;
72
73 //图象生效
74 g.dispose();
75 ByteArrayInputStream input=null;
76 ByteArrayOutputStream output = new ByteArrayOutputStream();
77 try{
78 ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
79 ImageIO.write(image, "JPEG", imageOut);
80 imageOut.close();
81 input = new ByteArrayInputStream(output.toByteArray());
82 }catch(Exception e){
83 System.out.println("验证码图片产生出现错误:"+e.toString());
84 }
85
86 this.image=input;/* 赋值图像 */
87 }
88 /*
89 * 给定范围获得随机颜色
90 */
91 private Color getRandColor(int fc,int bc){
92 Random random = new Random();
93 if(fc>255) fc=255;
94 if(bc>255) bc=255;
95 int r=fc+random.nextInt(bc-fc);
96 int g=fc+random.nextInt(bc-fc);
97 int b=fc+random.nextInt(bc-fc);
98 return new Color(r,g,b);
99 }
100 }
第二步,定义一个验证码输出的action
1 import java.io.ByteArrayInputStream;
2 import java.io.ByteArrayOutputStream;
3
4 import org.springframework.context.annotation.Scope;
5 import org.springframework.stereotype.Controller;
6
7 import com.cn.hospital.util.RandomCharUtil;
8 import com.cn.hospital.util.RandomNumUtil;
9 import com.opensymphony.xwork2.ActionContext;
10 import com.opensymphony.xwork2.ActionSupport;
11
12 @ Controller("utilAction")
13
14 @Scop e("prototype")
15
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 }
第三步,配置 struts.xml
<!-- 产生随机验证码 -->
<action name="randNum" class="utilAction" method="validNumGenerate">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
</result>
</action>
浙公网安备 33010602011771号