在webwork中使用自定义的Result生成动态验证图片

这个动态图片的实现原理是在servlet的response中写入一个ImageOutputStream,并由servlet容器将其转成图片,在非webwork的实现中,可以直接操作response,但是在webwork中,要想直接操作response的output则必须使用不需要对response操作的result类型

实现一个Result

<o:p> </o:p>

不可以用普通的dispatcherResultresponseoutputStream中写入东西,否则将覆盖所有的dispatcherjsp页面

<o:p>上次的代码忘记加上response的设置不缓存了,这样即使使用IE的回退也会刷新图片 

     private  HttpSession            session;

    
/**
     * 
@see  com.opensymphony.webwork.dispatcher.WebWorkResultSupport#doExecute(java.lang.String,
     *      com.opensymphony.xwork.ActionInvocation)
     
*/
    @Override
    
protected   void  doExecute(String finalLocation, ActionInvocation invocation)  throws  Exception
    {
        HttpServletRequest request 
=  (HttpServletRequest) invocation.getInvocationContext().get(
                ServletActionContext.HTTP_REQUEST);
        HttpServletResponse response 
=  (HttpServletResponse) invocation.getInvocationContext().get(
                ServletActionContext.HTTP_RESPONSE);
        response.setHeader(
" Pragma " " No-cache " );
        response.setHeader(
" Cache-Control " " no-cache " );
        response.setDateHeader(
" Expires " 0 );
        VerifyImage verify 
=   new  VerifyImage();
        OutputStream os 
=  response.getOutputStream();
        String str 
=  verify.GetImage(os);
        session 
=  request.getSession( true );
        session.setAttribute(
" rand " , str);
    }


</o:p><o:p> </o:p>

xwork.xml中配置result-type

        <result-types><o:p></o:p>

            <result-type name="image" <o:p></o:p>

              class="com.bnt.afp.action.verify.ImageResult"/><o:p></o:p>

        </result-types><o:p></o:p>

<o:p> </o:p>

添加一个生成图片的action

        <action name="imageAction"<o:p></o:p>

class="com.bnt.afp.action.verify.ImageAction"><o:p></o:p>

            <result name="success" type="image"/><o:p></o:p>

        </action><o:p></o:p>

<o:p> </o:p>

在需要生成验证图片的地方这样调用:

<img border=0 src="imageAction.action">

<o:p>
 ImageAction里只要简单的返回SUCCESS就可以了
</o:p>

     public  String execute()  throws  IOException
    {
        
return  SUCCESS;
    }



VerifyImage中生成图片的方法:(来自网上一个JSP生成动态验证图片的实例)

        // 获取生成的图片,返回生成的验证码,并将ImageOutputStream写入

       
public  String GetImage(OutputStream outputStream){

              

              
int  width = 60 , height = 20 ;

              BufferedImage image 
=   new  BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

              Graphics g 
=  image.getGraphics();

              Random random 
=   new  Random();

              g.setColor(getRandColor(
200 , 250 ));

              g.fillRect(
0 0 , width, height);

              g.setFont(
new  Font( " Times New Roman " ,Font.PLAIN, 18 ));

              

              g.setColor(getRandColor(
160 , 200 ));

              
for  ( int  i = 0 ;i < 155 ;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);

              }

              String sRand
= "" ;

              
for  ( int  i = 0 ;i < 4 ;i ++ ){

                  String rand
= String.valueOf(random.nextInt( 10 ));

                  sRand
+= rand;

                  g.setColor(
new  Color( 20 + random.nextInt( 110 ), 20 + random.nextInt( 110 ), 20 + random.nextInt( 110 )));

g.drawString(rand,
13 * i + 6 , 16 );

              }

              g.dispose();

              
try  {

                     ImageIO.write(image, 
" JPEG " , outputStream);

                     outputStream.flush();

                     
return  sRand;

              } 
catch  (IOException e) {

                     e.printStackTrace();

                     
return   " fail " ;

              }

       }

 

       
public  Color getRandColor( int  fc, int  bc){

              Random random 
=   new  Random();

        
if (fc > 255 ) fc = 255 ;

        
if (bc > 255 ) bc = 255 ;

        
int  r = fc + random.nextInt(bc - fc);

        
int  g = fc + random.nextInt(bc - fc);

        
int  b = fc + random.nextInt(bc - fc);

        
return   new  Color(r,g,b);

       }


                                                                                                                                 转载请注明作者和来源. 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值