-- 用于生成验证码
public class RandomNumUtil { private ByteArrayInputStream image;// 图像 private String str;// 验证码 public RandomNumUtil() { init(); } private void init() { // 在内存中创建图象 int width = 85, height = 20; BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); // 获取图形上下文 Graphics g = image.getGraphics(); // 生成随机类 Random random = new Random(); // 设定背景色 g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); // 设定字体 g.setFont(new Font("Times New Roman", Font.PLAIN, 12)); // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 g.setColor(getRandColor(160, 200)); for (int i = 0; i < 155; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } // 取随机产生的认证码(6位数字) String sRand = ""; for (int i = 0; i < 6; i++) { String rand = getVerify(random.nextInt(3)); sRand += rand; // 将认证码显示到图象中 g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 g.drawString(rand, 13 * i + 6, random.nextInt(10) + 10); } // 赋值验证码 this.str = sRand; // 图象生效 g.dispose(); ByteArrayInputStream input = null; ByteArrayOutputStream output = new ByteArrayOutputStream(); try { ImageOutputStream imageOut = ImageIO.createImageOutputStream(output); ImageIO.write(image, "JPEG", imageOut); imageOut.close(); input = new ByteArrayInputStream(output.toByteArray()); } catch (Exception e) { System.out.println("验证码图片产生出现错误:" + e.toString()); } this.image = input;/* 赋值图像 */ } /* 随机产生颜色 */ private Color getRandColor(int fc, int bc) { Random random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } /** * 生成验证码 * @param i 0为小写字母 * 1 为大写字母 * 2为普通数字 * @return */ private String getVerify(int i){ String str = ""; if(i == 0){ char c = 'a'; c=(char)(c+(int)(Math.random()*26)); str = c + ""; }else if(i == 1){ char c='A'; c=(char)(c+(int)(Math.random()*26)); str = c + ""; }else if(i == 2){ str = new Random().nextInt(10) + ""; } return str; } public ByteArrayInputStream getImage() { return image; } public void setImage(ByteArrayInputStream image) { this.image = image; } public String getStr() { return str; } public void setStr(String str) { this.str = str; }
-- Struts2
public class UtilAction extends ActionSupport { private ByteArrayInputStream inputStream; @Override public String execute() throws Exception { // TODO Auto-generated method stub RandomNumUtil rdnu = new RandomNumUtil(); this.setInputStream(rdnu.getImage());//取得带有随机字符串的图片 ActionContext.getContext().getSession().put("validateCode", rdnu.getStr());//取得随机字符串放入HttpSession return SUCCESS; } public ByteArrayInputStream getInputStream() { return inputStream; } public void setInputStream(ByteArrayInputStream inputStream) { this.inputStream = inputStream; } }
-- JQuery
$(function() { $("#randomCode").attr("src","verify"); $("#refresh").click( function() { $("#randomCode").attr("src","verify"); }); })
-- Struts2 xml配置
<action name="verify" class="verify.UtilAction"> <result type="stream"> <param name="contentType">image/jpeg</param> <param name="inputName">inputStream</param> </result> </action>
-- jsp页面
<body> <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="" width=150 height="50" alt="验证码图片" id="randomCode" /> <span id="refresh" style="cursor: pointer;">换张图片</span> </body>
-
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦