struts2实现图片验证码
生成图片验证码的主要工具类方法为:
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实现图片验证码的更多相关文章
- web开发(十) struts2之图片验证码
1.配置前端页面 <!-- 验证码--> <div class="form-group " style="padding-left: 9%;" ...
- 图片验证码(Struts2中使用)
写在前面: 最近在项目中做了一个登录页面,用到了图片验证码的功能,所以记录一下.方便之后再有用到,直接拿来用即可.其实图片验证码的生成都是有固定步骤的,网上也有很多的例子,有的时候,如果不想深究,都是 ...
- struts2生成随机验证码图片
之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...
- struts向网页输出图片验证码
前言:今天做个功能需要展示图片到页面,并不是下载,在网上搜了老半天,大部分都是下载,有的话也是只能在IE下进行输出,其它浏览器就都是下载了. Action代码: public String proce ...
- 字符型图片验证码识别完整过程及Python实现
字符型图片验证码识别完整过程及Python实现 1 摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...
- android图片验证码--自绘控件
自绘控件的内容都是自己绘制出来的 大致流程如下: 1.定义一个类继承view 使用TypedArray初始化属性集合 在view的构造方法中 有一个AttributeSet的参数 很明显是用来保存控件 ...
- webform(十)——图片水印和图片验证码
两者都需要引入命名空间:using System.Drawing; 一.图片水印 前台Photoshuiyin.aspx代码: <div> <asp:FileUpload ID=&q ...
- Android-简单的图片验证码
Android-图片验证码生成1.为啥要验证码?图片验证码在网络中使用的是比较普遍的.一般都是用来防止恶意破解密码.刷票.论坛灌水.刷页等.2.怎样的验证码比较好?验证码的获取方式无非就两种,一种是后 ...
- 在mvc中实现图片验证码的刷新
首先,在项目模型(Model)层中建立一个生成图片验证码的类ValidationCodeHelper,代码如下: public class ValidationCodeHelper { //用户存取验 ...
随机推荐
- python第五周:模块、标准库
模块相关知识: 定义:用来从逻辑上组织python代码(变量.函数.类.逻辑:实现一个功能)本质就是以.py结尾的python文件(文件名:test.py,对应的模块名:test) 附注:包:是用来从 ...
- linux 下password加密程序(能够用于替换shadow文件里的用户password)
源代码例如以下: #include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]){ if(arg ...
- cocos2d-x之浅析Hello World
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Word技巧杂记(一)——去掉页眉上方的黑线
今天在调整文章的格式时,突然发现在页眉的上方有一条巨粗无比的黑线,不知从何处冒出来的(如下图) 经过长时间的研究,终于发现原来这是页面的边框.解决办法也很简单: 格式->边框与底纹->页面 ...
- SQL Server loop - how do I loop through a set of records
SQL Server loop - how do I loop through a set of records By using T-SQL and cursors like this : DECL ...
- php静态函数的使用场景
php静态函数的使用场景 场景 代码 <?php class Conductor{ public static $i = 100; public function sold(){ $a = se ...
- 似然函数(likelihood function)
1. 似然函数基本定义 令 X1,X2,-,Xn 为联合密度函数 f(X1,X2,-,Xn|θ),给定观测值 X1=x1,X2=x2,-,Xn=xn,关于 θ 的似然函数(likelihood fun ...
- Oracle在Linux下的性能优化
Oracle数据库内存参数的优化 Ø 与oracle相关的系统内核参数 Ø SGA.PGA参数设置 Oracle下磁盘存储性能优化 Ø 文件系统的选择(ext2 ...
- python中黏包现象
#黏包:发送端发送数据,接收端不知道应如何去接收造成的一种数据混乱现象. #关于分包和黏包: #黏包:发送端发送两个字符串"hello"和"word",接收方却 ...
- Spring学习笔记(二) 初探Spring
版权声明 笔记出自<Spring 开发指南>一书. Spring 初探 前面我们简单介绍了 Spring 的基本组件和功能,现在我们来看一个简单示例: Person接口Person接口定义 ...