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 { //用户存取验 ...
随机推荐
- 【ACM-ICPC 2018 南京赛区网络预赛 I】Skr
[链接] 我是链接,点我呀:) [题意] 让你把一个字符串的所有回文子串加起来.(当做数字加起来) 求他们的和. [题解] 回文树. 从两个根节点分别遍历整棵回文树. 按照每个节点的定义. 得到每个节 ...
- ElasticSearch 深度分页解决方案
常见深度分页方式 from+size 另一种分页方式 scroll scroll + scan search_after 的方式 es 库 scroll search 的实现 常见深度分页方式 fro ...
- BIRT报表Cannot open the connection for the driver:org.eclipse.birt.report.data.oda.jdbc.dbprofile
现象:报表不能打开: 找了半个小时,随手改了一下tomcat/conf文件夹下的context.xml文件 <Context xmlBlockExternal="false" ...
- 近200篇机器学习&深度学习资料分享
编者按:本文收集了百来篇关于机器学习和深度学习的资料,含各种文档,视频,源码等.并且原文也会不定期的更新.望看到文章的朋友能够学到很多其它. <Brief History of Machine ...
- 通过meta标签改变浏览器内核做兼容
<meta name="renderer" content="webkit|ie-stand|ie-comp" /> <meta http-e ...
- 2015.06.11,技术,关于Matlab中的Jbtest检验
总体分布的正态性检验一般采取Jarque-Bera检验方法. 1. JBTest检验的定义: 在统计学中,Jarque-Bera检验是对样本数据是否具有符合正态分布的偏度和峰度的拟合优度的检验.该检验 ...
- UVA - 12230 Crossing Rivers 概率期望
You live in a village but work in another village. You decided to follow the straight path between y ...
- [Javascript] 层控制(隐藏、显示、卷起、展开)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 18. 4Sum[M]四数之和
题目 Given an array nums of n integers and an integer target, are there elements a, b, c and d in nums ...
- sqlserver数据文件位置如何迁移
亲测有效的一种方式: 1.对应的数据库脱机 2.迁移物理文件 3.删除原有实例 4.附加