action中生成验证码图片

1、login.jsp页面程序 
<script type="text/javascript">    
function changeValidateCode(obj) {    
//获取当前的时间作为参数,无具体意义    
var timenow = new Date().getTime();    
//每次请求需要一个不同的参数,否则可能会返回同样的验证码    
//这和浏览器的缓存机制有关系,也可以把页面设置为不缓存,这样就不用这个参数了。    
obj.src="rand.action?d="+timenow;    
}    
</script>    
在表单中添加下面这句话:    
<s:text name="random"></s:text>:<s:textfield name="rand" size="5"></s:textfield><img src="rand.action"   onclick="changeValidateCode(this)" title="点击图片刷新验证码"/>   

2.struts中

<!-- Random验证码 -->    
<action name="rand" class="com.mxl.rand.RandomAction">       
        <result type="stream">       
             <param name="contentType">image/jpeg</param>       
             <param name="inputName">inputStream</param>       
        </result>    
    </action>   

/**
*
*/
private static final long serialVersionUID = 355895120853966170L;
/**
* JSP内置对象session
*/
private Map<String, Object> session;
/**
* 字节输入流
*/
private ByteArrayInputStream inputStream;

public ByteArrayInputStream getInputStream() {
return inputStream;
}

public void setInputStream(ByteArrayInputStream inputStream) {
this.inputStream = inputStream;
}

/**
* 设置session
*/
@Override
public void setSession(Map<String, Object> sess) {
this.session = sess;
}

/**
* 获得验证码
*
* @return 返回4位字母、数字
*/
private String getRandomCode() {
StringBuffer sb = new StringBuffer();
Random r = new Random();
for (int i = 0; i < 4; i++) {
switch (r.nextInt(3)) {
case 0:
// 生成数字
sb.append((char) (48 + r.nextInt(10)));
break;
case 1:
// 生成大写字母
sb.append((char) (65 + r.nextInt(26)));
break;
case 2:
// 生成小写字母
sb.append((char) (97 + r.nextInt(26)));
break;
}
}
return sb.toString();
}

/**
* 获得随机颜色
*
* @return 返回RGB颜色
*/
private Color getRandomColor() {
Random random = new Random();
int r = random.nextInt(256);
int g = random.nextInt(256);
int b = random.nextInt(256);
return new Color(r, g, b);
}

/**
* 根据字符串 生成图片
*
* @param code
* 随机字符串
* @return 内存中生成的验证码图片
*/
private BufferedImage getImage(String code) {
int width = 75, height = 30;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 生成随机类
Random random = new Random();
// 设定背景色
g.setColor(new Color(240, 240, 240));
g.fillRect(0, 0, width, height);

// 画边框
g.setColor(this.getRandomColor());
g.drawRect(0, 0, width - 1, height - 1);
// 随机产生干扰线
g.setColor(this.getRandomColor());
for (int i = 0; i < 15; 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);
}
// 设定字体
g.setFont(new Font("Verdana", Font.PLAIN, 18));
char[] chs = code.toCharArray();
g.drawString(String.valueOf(chs[0]), 5, 20);
g.drawString(String.valueOf(chs[1]), 22, 20);
g.drawString(String.valueOf(chs[2]), 39, 20);
g.drawString(String.valueOf(chs[3]), 56, 20);
// 图象生效
g.dispose();
return image;
}

/**
* 将图片以字节的形式写入到InputStream
*
* @return
*/
private ByteArrayInputStream createInputStream() {
// 生成验证码
String code = this.getRandomCode();
BufferedImage image = this.getImage(code);
session.put("code", code);
// 输出到文件
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageOutputStream imageOut = null;
ByteArrayInputStream inStream = null;
try {
imageOut = ImageIO.createImageOutputStream(output);
ImageIO.write(image, "jpeg", imageOut);
imageOut.close();
inStream = new ByteArrayInputStream(output.toByteArray());
output.close();
} catch (IOException e) {
System.out.println("验证码图片生成失败...");
e.printStackTrace();
}
return inStream;
}

@Override
public String execute() throws Exception {
this.setInputStream(this.createInputStream());
return SUCCESS;
}

破罐子互摔
原文地址:https://www.cnblogs.com/zonglonglong/p/2664850.html