struts2+spring生成验证码问题

08-02-19 dearfashion

问题:重复调用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" class="com.dearfashion.eshop.security.action.AccountAction" 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);

banq
2008-02-20 18:00

验证码相同,和具体框架没有关系,是http协议的cache等方面原因,需要设置服务器端,或者通过response将cache清除。

bigsaturn
2008-05-18 20:28

有可能你在Spring中把Action配置成单例了