生成图片验证码的主要工具类方法为:

 package com.yeting.fc.util;

 import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random; import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream; public class ImageCodeUtil {
/**
* 生成随机验证码字符串
*
* @return
*/
public static String getImageCodeStr() { Random random = new Random();
String code[] = { "A", "a", "B", "b", "C", "c", "D", "d", "E", "e",
"F", "f", "G", "g", "H", "h", "I", "i", "J", "j", "K", "k",
"L", "l", "M", "m", "N", "n", "O", "o", "P", "p", "Q", "q",
"R", "r", "S", "s", "T", "t", "U", "u", "V", "v", "W", "w",
"X", "x", "Y", "y", "Z", "z", "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "1",
"2", "3", "4", "5", "6", "7", "8", "9", "0", "1" };
// 取随机产生的认证码(4位字符)
StringBuffer codeStr = new StringBuffer("");
for (int i = 0; i < 4; i++) {
String cStr = code[random.nextInt(104)];
codeStr.append(cStr);
}
return codeStr.toString();
} /**
* 生成带随机验证码的图片
*
* @param codeStr
* @return
*/
public static BufferedImage createImage(String codeStr) {
int width = 60, 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, 18));
// 随机产生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);
}
for (int i = 0; i < codeStr.length(); i++) {
String cStr = codeStr.charAt(i) + "";
g.setColor(new Color(random.nextInt(125), random.nextInt(125),
random.nextInt(125)));
g.setFont(new Font("", Font.PLAIN, 20 + random.nextInt(5)));
g.drawString(cStr, 15 * i + random.nextInt(5),
20 - random.nextInt(5));
}
g.dispose();
return image;
} /**
* 返回验证码图片的流格式
*
* @param codeStr
* @return
*/
public static ByteArrayInputStream getImageAsInputStream(String codeStr) {
BufferedImage image = createImage(codeStr);
return convertImageToStream(image);
} private static ByteArrayInputStream convertImageToStream(BufferedImage image) {
ByteArrayInputStream inputStream = null;
ByteArrayOutputStream output = null;
ImageOutputStream imageOut = null;
try {
output = new ByteArrayOutputStream();
imageOut = ImageIO.createImageOutputStream(output);
ImageIO.write(image, "JPEG", imageOut);
inputStream = new ByteArrayInputStream(output.toByteArray());
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(imageOut!=null){
imageOut.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return inputStream;
} /*
* 给定范围获得随机颜色
*/
private static 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);
} }

Action中主要代码为:

package com.yeting.fc.action;

import java.io.ByteArrayInputStream;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yeting.fc.util.ImageCodeUtil; public class ImageCodeAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private ByteArrayInputStream inputStream;
public String getImageCode() throws Exception{
//获取图片字符串
String codeStr = ImageCodeUtil.getImageCodeStr();
ActionContext.getContext().getSession().put("rand",codeStr);
//System.out.println(codeStr);
inputStream = ImageCodeUtil.getImageAsInputStream(codeStr);
return SUCCESS;
} public void setInputStream(ByteArrayInputStream inputStream) {
this.inputStream = inputStream;
}
public ByteArrayInputStream getInputStream() {
return inputStream;
} }

struts.xml文件中配置如下:

 <!-- 验证码Action -->
<action name="imageCode" class="imageCodeAction" method="getImageCode">
<result type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
</result>
</action>

登陆页面:主要代码。

 <!--刷新请求,更换验证码内容。-->
<script type="text/javascript">
function imageCode(){
document.getElementById("imageCode").src="${pageContext.request.contextPath }/yeting/imageCode.action?st="+new Date();
}
</script> <!--验证码主要html文件--> 验证码:<input style="width:50px;" type="text" name="imageCode"/>
<a href="javascript:imageCode()">
<img style="margin-top: 5px;" id="imageCode" src="${pageContext.request.contextPath }/yeting/imageCode.action"/>
</a>
<a href="javascript:imageCode()"><input style="width:35px;" type="button" value="更换"/></a>

struts2实现图片验证码的更多相关文章

  1. web开发(十) struts2之图片验证码

    1.配置前端页面 <!-- 验证码--> <div class="form-group " style="padding-left: 9%;" ...

  2. 图片验证码(Struts2中使用)

    写在前面: 最近在项目中做了一个登录页面,用到了图片验证码的功能,所以记录一下.方便之后再有用到,直接拿来用即可.其实图片验证码的生成都是有固定步骤的,网上也有很多的例子,有的时候,如果不想深究,都是 ...

  3. struts2生成随机验证码图片

    之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...

  4. struts向网页输出图片验证码

    前言:今天做个功能需要展示图片到页面,并不是下载,在网上搜了老半天,大部分都是下载,有的话也是只能在IE下进行输出,其它浏览器就都是下载了. Action代码: public String proce ...

  5. 字符型图片验证码识别完整过程及Python实现

    字符型图片验证码识别完整过程及Python实现 1   摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...

  6. android图片验证码--自绘控件

    自绘控件的内容都是自己绘制出来的 大致流程如下: 1.定义一个类继承view 使用TypedArray初始化属性集合 在view的构造方法中 有一个AttributeSet的参数 很明显是用来保存控件 ...

  7. webform(十)——图片水印和图片验证码

    两者都需要引入命名空间:using System.Drawing; 一.图片水印 前台Photoshuiyin.aspx代码: <div> <asp:FileUpload ID=&q ...

  8. Android-简单的图片验证码

    Android-图片验证码生成1.为啥要验证码?图片验证码在网络中使用的是比较普遍的.一般都是用来防止恶意破解密码.刷票.论坛灌水.刷页等.2.怎样的验证码比较好?验证码的获取方式无非就两种,一种是后 ...

  9. 在mvc中实现图片验证码的刷新

    首先,在项目模型(Model)层中建立一个生成图片验证码的类ValidationCodeHelper,代码如下: public class ValidationCodeHelper { //用户存取验 ...

随机推荐

  1. 在windows环境中关于 pycharm配置 anaconda 虚拟环境

    因为要在windows系统系统中练习tensorflow,所以需要配置一下环境(来回的开关机切换环境太麻烦了......) 首先安装anaconda3,我选择的版本是Anaconda3 5.1.0,对 ...

  2. Android面试题集

    前几天整理了Java面试题集合,今天再来整理下Android相关的面试题集合.假设你希望能得到最新的消息,能够关注https://github.com/closedevice/interview-ab ...

  3. Openwrt 软件安装源

    进入http://downloads.openwrt.org/barrier_breaker/14.07/站点找到符合处理器型号的软件源.參考下图: watermark/2/text/aHR0cDov ...

  4. HDU 4912 lca贪心

    Paths on the tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  5. How to remove focus without setting focus to another control?

    How to remove focus without setting focus to another control? Ask Question up vote 67 down vote favo ...

  6. Swift - 分页菜单的实现(使用PagingMenuController库实现tab标签切换)

    分页菜单(分段菜单)在许多 App 上都会用到.比如大多数新闻 App,如网易新闻.今日头条等,顶部都有个导航菜单.这个导航菜单是一组标签的集合,每个标签表示一个新闻类别,我们点击这个标签后下面就会切 ...

  7. WebBrowser网页操作之提取获取元素和标签(完整篇)

    最近使用WebBrower做了几个Hook小程序,收集积累如下: using System; using System.Collections.Generic; using System.Linq; ...

  8. js最简单的-点击小图放大

    js最简单的-点击小图放大 标签(空格分隔): js <html> <body> <img class="imgview" src="{$v ...

  9. 9. Palindrome Number[E]回文数

    题目 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same b ...

  10. flask之jinji2模板介绍

    1.1.模板传参 (1)主程序   from flask import Flask,render_template app = Flask(__name__) @app.route('/') def ...