1.驗證碼生成工具類java
1 package cn.bd.jboa.util; 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.io.ByteArrayInputStream; 8 import java.io.ByteArrayOutputStream; 9 import java.util.Random; 10 import javax.imageio.ImageIO; 11 import javax.imageio.stream.ImageOutputStream; 12 13 public class RandomNumUtil { 14 private ByteArrayInputStream image;// 圖像 15 private String str;// 驗證碼 16 17 private RandomNumUtil() { 18 init();// 初始化屬性 19 } 20 21 /* 22 * 取得RandomNumUtil實例 23 */ 24 public static RandomNumUtil Instance() { 25 return new RandomNumUtil(); 26 } 27 28 /* 29 * 取得驗證碼圖片 30 */ 31 public ByteArrayInputStream getImage() { 32 return this.image; 33 } 34 35 /* 36 * 取得圖片的驗證碼 37 */ 38 public String getString() { 39 return this.str; 40 } 41 42 private void init() { 43 // 在內存中建立圖象 44 int width = 85, height = 20; 45 BufferedImage image = new BufferedImage(width, height, 46 BufferedImage.TYPE_INT_RGB); 47 // 獲取圖形上下文 48 Graphics g = image.getGraphics(); 49 // 生成隨機類 50 Random random = new Random(); 51 // 設定背景色 52 g.setColor(getRandColor(200, 250)); 53 g.fillRect(0, 0, width, height); 54 // 設定字體 55 g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); 56 // 隨機產生155條幹擾線,使圖象中的認證碼不易被其它程序探測到 57 g.setColor(getRandColor(160, 200)); 58 for (int i = 0; i < 155; i++) { 59 int x = random.nextInt(width); 60 int y = random.nextInt(height); 61 int xl = random.nextInt(12); 62 int yl = random.nextInt(12); 63 g.drawLine(x, y, x + xl, y + yl); 64 } 65 // 取隨機產生的認證碼(6位數字) 66 String sRand = ""; 67 for (int i = 0; i < 6; i++) { 68 String rand = String.valueOf(random.nextInt(10)); 69 sRand += rand; 70 // 將認證碼顯示到圖象中 71 g.setColor(new Color(20 + random.nextInt(110), 20 + random 72 .nextInt(110), 20 + random.nextInt(110))); 73 // 調用函數出來的顏色相同,多是由於種子太接近,因此只能直接生成 74 g.drawString(rand, 13 * i + 6, 16); 75 } 76 // 賦值驗證碼 77 this.str = sRand; 78 79 // 圖象生效 80 g.dispose(); 81 ByteArrayInputStream input = null; 82 ByteArrayOutputStream output = new ByteArrayOutputStream(); 83 try { 84 ImageOutputStream imageOut = ImageIO 85 .createImageOutputStream(output); 86 ImageIO.write(image, "JPEG", imageOut); 87 imageOut.close(); 88 input = new ByteArrayInputStream(output.toByteArray()); 89 } catch (Exception e) { 90 System.out.println("驗證碼圖片產生出現錯誤:" + e.toString()); 91 } 92 93 this.image = input;/* 賦值圖像 */ 94 } 95 96 /* 97 * 給定範圍得到隨機顏色 98 */ 99 private Color getRandColor(int fc, int bc) { 100 Random random = new Random(); 101 if (fc > 255) 102 fc = 255; 103 if (bc > 255) 104 bc = 255; 105 int r = fc + random.nextInt(bc - fc); 106 int g = fc + random.nextInt(bc - fc); 107 int b = fc + random.nextInt(bc - fc); 108 return new Color(r, g, b); 109 } 110 }
2.actionjson
1 package cn.bd.jboa.action; 2 3 import java.io.ByteArrayInputStream; 4 import cn.bd.jboa.util.RandomNumUtil; 5 import com.opensymphony.xwork2.ActionContext; 6 import com.opensymphony.xwork2.ActionSupport; 7 8 /** 9 * 驗證碼 10 * @author TaoXianXue 11 * 12 */ 13 public class RandomAction extends ActionSupport { 14 15 private static final long serialVersionUID = 1L; 16 private ByteArrayInputStream inputStream; 17 18 // 生成驗證碼方法 19 public String execute() throws Exception { 20 RandomNumUtil rdnu = RandomNumUtil.Instance(); 21 this.setInputStream(rdnu.getImage());/// 取得帶有隨機字符串的圖片 22 ActionContext.getContext().getSession().put("random", rdnu.getString());//把驗證碼放入HttpSession中 23 return "random"; 24 } 25 26 public void setInputStream(ByteArrayInputStream inputStream) { 27 this.inputStream = inputStream; 28 } 29 30 public ByteArrayInputStream getInputStream() { 31 return inputStream; 32 } 33 }
3.struts.xmldom
1 <!-- 驗證碼操做Action配置 --> 2 <package name="random" extends="json-default"> 3 <!-- Random驗證碼 --> 4 <action name="random" class="randomAction"> 5 <result type="stream" name="random"> 6 <param name="contentType">image/jpeg</param> 7 <param name="inputName">inputStream</param> 8 </result> 9 </action> 10 </package>
4.jsp頁面jsp
1 <input type="image" src="random.action" onclick="this.src=this.src+'?'" title="點擊圖片刷新驗證碼" /> 函數