Struts2图片验证码
来源:互联网 发布:阿里云虚拟主机修改php 编辑:程序博客网 时间:2024/05/20 00:49
1.随机字符生成
package com.future.xdzy.util;public class VerifyCode { private char getCapitalLetter(){ return (char)((int)(Math.random()*26)+'A'); } private char getLowercaseLetter(){ return (char)((int)(Math.random()*26)+'a'); } private char getNumber(){ return (char)((int)(Math.random()*10)+'0'); } private char getSymbol(){ char[] symbols={'(',')','[',']','{','}','%','@'}; int index=(int)(Math.random()*symbols.length); return symbols[index]; } private char getChar(){ int type=(int)(Math.random()*4); if(type==0){ return getCapitalLetter(); }else if(type==1){ return getLowercaseLetter(); }else if(type==2){ return getNumber(); }else{ return getSymbol(); } } public String getVerifyCode(int len){ String code=""; for(int i=0;i<len;i++){ code+=getChar()+""; } return code; }}
2.图片生成
package com.future.xdzy.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.io.IOException;import javax.imageio.ImageIO;public class VerifyIamge { private static ByteArrayOutputStream outputStream; private static ByteArrayInputStream inputStream; private static BufferedImage CreatImage(){ String code=new VerifyCode().getVerifyCode(6); int font_size=15; int width=font_size*code.length(); int height=font_size*2; BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g=image.createGraphics(); //背景 g.setColor(Color.white); g.fillRect(0, 0, width, height); //边框 g.setColor(Color.green); g.drawRect(0, 0, width-1, height-1); //字符 g.setColor(Color.BLUE); g.setFont(new Font("微软雅黑",Font.BOLD,font_size)); g.drawString(code, 15, height/2+3); //噪点 g.setColor(Color.RED); for(int i=0;i<code.length()*10;i++){ int x=(int)(Math.random()*width); int y=(int)(Math.random()*height); g.fillRect(x, y, 1, 1); } g.dispose(); return image; } public static ByteArrayInputStream getStream() throws IOException{ BufferedImage image=CreatImage(); outputStream=new ByteArrayOutputStream(); ImageIO.write(image, "jpeg", outputStream); inputStream = new ByteArrayInputStream(outputStream.toByteArray()); return inputStream; } public static void closeStream() throws IOException{ inputStream.close(); outputStream.close(); }}
3.Ation调用
package com.future.xdzy.action.util;import java.io.ByteArrayInputStream;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.future.xdzy.util.VerifyIamge;import com.opensymphony.xwork2.ActionSupport;public class CreatVerifyImage extends ActionSupport { private static final long serialVersionUID = 1L; private ByteArrayInputStream inputStream; public ByteArrayInputStream getInputStream() { return inputStream; } public void setInputStream(ByteArrayInputStream inputStream) { this.inputStream = inputStream; } @Override public String execute() throws Exception { HttpServletResponse response=ServletActionContext.getResponse(); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); ByteArrayInputStream input=VerifyIamge.getStream(); this.setInputStream(input); VerifyIamge.closeStream(); return SUCCESS; }}
4.Action配置
<package name="ActionUtil" namespace="/util" extends="struts-default"> <action name="VerifyImage" class="com.future.xdzy.action.util.CreatVerifyImage"> <result name="success" type="stream"> <param name="contentType">image/jpeg</param> <param name="inputName">inputStream</param> </result> </action></package>
5.页面调用
<img alt="" src="./util/VerifyImage" onclick="this.src='./util/VerifyImage?'+ Math.random()">
0 0
- Struts2 图片验证码
- Struts2 图片验证码
- Struts2 ----图片验证码
- Struts2 验证码图片
- Struts2-图片验证码
- Struts2图片验证码
- struts2 实现图片验证码
- struts2 实现图片验证码
- Struts2 验证码图片实例
- Struts2 验证码图片实例
- Struts2 验证码图片实例
- Struts2 验证码图片实例
- Struts2 验证码图片实例
- Struts2 验证码图片实例
- Struts2 验证码图片实例
- Struts2 验证码图片实例
- Struts2 验证码图片实例
- Struts2 验证码图片实例
- win10 安装teamtoy
- 脚本周期
- QT之emit,signals和slots
- Android开发艺术探索》综合技术,JNI和性能优化小结
- How to set up Liferay DXP/7 on Linux
- Struts2图片验证码
- 如何putty安装php, apache, mysql(LAMP), 连接digital ocean server, 将自己的网站po上server
- 为什么要用session
- structs2学习(二):使用默认action
- 07 CSS-相对定位、绝对定位、固定定位、z-index
- 工作中常常使用的几种加密以及签名的方式:32位MD5
- 初探RecyclerView开发
- 微信6.0主界面的安卓实现
- HDFS介绍