您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
-- 用于生成验证码
- public class RandomNumUtil {
- private ByteArrayInputStream p_w_picpath;// 图像
- private String str;// 验证码
- public RandomNumUtil() {
- init();
- }
- private void init() {
- // 在内存中创建图象
- int width = 85, height = 20;
- BufferedImage p_w_picpath = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
- // 获取图形上下文
- Graphics g = p_w_picpath.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 p_w_picpathOut = ImageIO.createImageOutputStream(output);
- ImageIO.write(p_w_picpath, "JPEG", p_w_picpathOut);
- p_w_picpathOut.close();
- input = new ByteArrayInputStream(output.toByteArray());
- } catch (Exception e) {
- System.out.println("验证码图片产生出现错误:" + e.toString());
- }
- this.p_w_picpath = 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 p_w_picpath;
- }
- public void setImage(ByteArrayInputStream p_w_picpath) {
- this.p_w_picpath = p_w_picpath;
- }
- 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">p_w_picpath/jpeg</param>
- <param name="inputName">inputStream</param>
- </result>
- </action>
-- jsp页面
- <body>
- <img src="" width=150 height="50" alt="验证码图片" id="randomCode" />
- <span id="refresh" style="cursor: pointer;">换张图片</span>
- </body>
--效果如图
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。