import com.opensymphony.xwork2.ActionSupport;
import java.util.Random;
import java.awt.*;
import java.awt.image.*;
import java.io.ByteArrayOutputStream;
import org.apache.struts2.ServletActionContext;
public class ImageAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 验证码对应的Session名
*/
private static final String SESSIONNAME = "CheckCodeImageAction";
/**
* 用于随机生成验证码的数据源
*/
private static final char[] SOURCECODE = new char[]{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
};
/**
* 用于随机打印验证码的字符颜色
*/
private static final Color[] COLORS = new Color[]{
Color.RED, Color.BLUE, Color.BLACK, Color.GREEN, Color.YELLOW, Color.CYAN
};
/**
* 用于打印验证码的字体
*/
private static final Font FONTCODE = new Font("Times New Roman", Font.ITALIC, 25);
/**
* 用于生成随机数的随机数生成器
*/
private static final Random rdm = new Random();
private String text = "";
private byte[] bytes = null;
private String contentType = "image/png";
public byte[] getImageBytes(){
return this.bytes;
}
public String getContentType(){
return this.contentType;
}
public void setContentType(String value){
this.contentType = value;
}
public int getContentLength(){
return bytes.length;
}
/**
* 生成长度为4的随机字符串
*/
private void generateText(){
char[] source = new char[4];
for(int i=0; i<source.length; i++){
source[i] = ImageAction.SOURCECODE[rdm.nextInt(ImageAction.SOURCECODE.length)];
}
this.text = new String(source);
// 设置Session
ServletActionContext.getRequest().getSession().setAttribute(SESSIONNAME, this.text);
}
/**
* 在内存中生成打印了随机字符串的图片
* @return 在内存中创建的打印了字符串的图片
*/
private BufferedImage createImage(){
int width = 150;
int height = 45;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, width, height);
g.setFont(FONTCODE);
for(int i=0; i<this.text.length(); i++){
g.setColor(COLORS[rdm.nextInt(COLORS.length)]);
g.drawString(this.text.substring(i, i+1), 9 + rdm.nextInt(i + 5) * 11, 45-rdm.nextInt(7)*3 );
}
g.dispose();
return image;
}
/**
* 根据图片创建字节数组
* @param image 用于创建字节数组的图片
*/
private void generatorImageBytes(BufferedImage image){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try{
javax.imageio.ImageIO.write(image, "jpg", bos);
this.bytes = bos.toByteArray();
}catch(Exception ex){
ex.printStackTrace();
}finally{
try{
bos.close();
}catch(Exception ex1){
ex1.printStackTrace();
}
}
}
/**
* 被struts2过滤器调用的方法
* @return 永远返回字符串"image"
*/
public String getImage(){
this.generateText();
BufferedImage image = this.createImage();
this.generatorImageBytes(image);
return "image";
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.rs.learnOnline.action.ImageAction;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
public class ImageResult implements Result {
/**
*
*/
private static final long serialVersionUID = 5205515981838231120L;
public void execute(ActionInvocation ai) throws Exception {
ImageAction action = (ImageAction)ai.getAction();
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Cash", "no cash");
response.setContentType(action.getContentType());
response.setContentLength(action.getContentLength());
response.getOutputStream().write(action.getImageBytes());
response.getOutputStream().flush();
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<package namespace="/test" name="testPackage" extends="struts-default">
<result-types>
<result-type name="ValidateImage" class="com.rs.learnOnline.dao.impl.ImageResult" />
</result-types>
<action name="ValidateImage" class="com.rs.learnOnline.action.ImageAction" method="getImage">
<result name="image" type="ValidateImage" />
</action>
</package>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function changeValidateCode(obj) {
//获取当前的时间作为参数,无具体意义
var timenow = new Date().getTime();
//每次请求需要一个不同的参数,否则可能会返回同样的验证码
//可能和浏览器的缓存机制有关系
obj.src="test/ValidateImage?d="+timenow;
}
<td align="right">验证图片:</td>
<td><img id="vfcode" alt="操作频繁,稍后再试" src="test/ValidateImage" onclick="changeValidateCode(this)" title="看不清楚? 换张图片" />
</td>