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 { //用户存取验 ...
随机推荐
- yii2-dingtalk 钉钉群机器人
说明 群机器人是钉钉群的高级扩展功能.群机器人可以将第三方服务的信息聚合到群聊中,实现自动化的信息同步.目前,大部分机器人在添加后,还需要进行Webhook配置,才可正常使用(配置说明详见操作流程中的 ...
- Problem 1
Problem 1 # Problem_1.py """ If we list all the natural numbers below 10 that are mul ...
- CodeForcesGym 100548G The Problem to Slow Down You
The Problem to Slow Down You Time Limit: 20000ms Memory Limit: 524288KB This problem will be judged ...
- [Asp.net]EF更新之后要 保存
数据库那边保存表 项目里保存模型
- POJ 1320
作弊了--!该题可以通过因式分解得到一个佩尔方程....要不是学着这章,估计想不到.. 得到x1,y1后,就直接代入递推式递推了 x[n]=x[n-1]*x[1]+d*y[n-1]*y[1] y[n] ...
- Android 经常使用设计模式(一)
由于项目变更的频繁性,作为一名程序猿,我们须要掌握设计模式的必要性.就不言而喻~~.以下就是一些我自己学习的设计模式总结. 接下来,主要是针对几个比較经常使用模式进行解说,主要是以下几种: 观察者模式 ...
- 可编程数据平面将OpenFlow扩展至电信级应用(二)
可编程数据平面将OpenFlow扩展至电信级应用(二) 案例:基于WinPath网络处理器的电信极OpenFlow (CG-OF)client实现 作者:Liviu Pinchas, Tao Lang ...
- win7-32虚拟机安装
前置条件:安装好VMware-workstation 一.本人本机win7—32位 准备win7_32位镜像文件GSP1RMCULFRER_CN_DVD.iso 新建一个文件夹,将它保存在我们的新建文 ...
- poj - 1159 - Palindrome(滚动数组dp)
题意:一个长为N的字符串( 3 <= N <= 5000).问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->> ...
- Bitcoin学习篇之---PPS和PPLNS挖矿模式介绍
PPS和PPLNS挖矿模式介绍 比特币每10分钟产生一个区块,会有千万人竞争.而这个区块终于仅仅归1个人全部.其他人都颗粒无收. 你或许要挖5年才干获得一个区块. 组队挖矿就是.一旦队伍里不论什么人获 ...