关于struts2 验证码生成问题
问题:重复调用每次返回action的值相同
action:
================================================================
public class AccountAction extends ActionSupport {
/**
*生成验证图片的execute方法,
/*
public String validataImage(){
createInputStream(ValidateImageService.Disturb_Type_Complex);//生成图片inputStream
return SUCCESS;
}
/**
*生成图片的具体方法
/*
private String createInputStream(int disturbType) throws IOException{
ByteArrayOutputStream bos =new ByteArrayOutputStream();
String validateCode = null;
ValidateImageServiceImp validateImageService=new ValidateImageServiceImp();
validateCode = validateImageService.createValidateCode(disturbType,18, bos, 100, 30, null, 5);//把验证码图片放入bos
inputStream = new ByteArrayInputStream(bos.toByteArray());
bos.close();
return validateCode;
}
}
struts.xml
========================================================================
<action name="validataImage" method="validataImage">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
</result>
</action>
jsp
====================================================
<img id="random" src="validataImage.action" />
<a href="#" onclick="javascript:document.getElementById('random').src='validataImage.action'">看不清楚</a>
==========================================
点击 ‘看不清楚’每次返回的结果是同一张图片,或者重复调用
<img id="random" src="validataImage.action" />
<img id="random" src="validataImage.action" />
多次,也是同一张图片,并且方法AccountAction.validataImage()只会执行一次
分析原因,可能是存在缓冲,也可能其他原因,如果直接用response的方式就没有问题
如:outStream = response.getOutputStream();
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(outStream);
encoder.encode(image);