1: jsp 页面.
<s:url action="va_ValidateImage" namespace="/user" id="validate"></s:url>
<img alt="F5刷新" src="${validate}" id="codeimg" style="width: 70px; height: 24px; border: 1px solid #abcedf;" />
2: struts.xml配置.
<!--自定义返回结果-->
<result-types>
<result-type name="ValidateImage" class="com.tiros.caredog.cd.util.ImageResult" />
</result-types>
<!--定义va_ValidateImage action-->
<action name="va_ValidateImage" class="com.tiros.caredog.cd.action.ImageAction" method="doDefault">
<result name="image" type="ValidateImage" />
</action>
3: 后台工具类.
3.1 自定义ImageResult类.
package com.tiros.caredog.cd.util;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.tiros.caredog.cd.action.ImageAction;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
/**
* image Result
* @author songzj
*
*/
public class ImageResult implements Result {
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();
response.getOutputStream().close();
}
}
3.2 被访问ImageAction类.
package com.tiros.caredog.cd.action;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Random;
import com.tiros.caredog.cd.util.Constent;
public class ImageAction extends CommonAction {
/**
*
* 用于随机生成验证码的数据源
*
*/
private static final char[] source = new char[] {
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
};
/**
*
* 用于随机打印验证码的字符颜色
*
*/
private static final Color[] colors = new Color[] {
Color.RED, Color.BLUE, Color.BLACK
};
/**
*
* 用于打印验证码的字体
*
*/
private static final Font font = new Font("宋体", Font.ITALIC, 15);
/**
*
* 用于生成随机数的随机数生成器
*
*/
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.source[rdm
.nextInt(ImageAction.source.length)];
}
this.text = new String(source);
// 设置Session
super.getSession().setAttribute(Constent.VALIDATE_CODE_KEY, this.text);
}
/**
*
* 在内存中生成打印了随机字符串的图片
*
* @return 在内存中创建的打印了字符串的图片
*
*/
private BufferedImage createImage() {
int width = 40;
int height = 15;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.setFont(font);
for (int i = 0; i < this.text.length(); i++) {
g.setColor(colors[rdm.nextInt(colors.length)]);
g.drawString(this.text.substring(i, i + 1), 2 + i * 8, 12);
}
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) {
} finally {
try {
bos.close();
} catch (Exception ex1) {
}
}
}
/**
*
* 被struts2过滤器调用的方法
*
* @return 永远返回字符串"image"
*
*/
public String doDefault() {
this.generateText();
BufferedImage image = this.createImage();
this.generatorImageBytes(image);
return "image";
}
}