<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 59, 文章 - 4, 評論 - 184, 引用 - 7
    數據加載中……

    在webwork中使用自定義的Result生成動態驗證圖片

    這個動態圖片的實現原理是在servlet的response中寫入一個ImageOutputStream,并由servlet容器將其轉成圖片,在非webwork的實現中,可以直接操作response,但是在webwork中,要想直接操作response的output則必須使用不需要對response操作的result類型

    實現一個
    Result

     

    不可以用普通的dispatcherResultresponseoutputStream中寫入東西,否則將覆蓋所有的dispatcherjsp頁面

    上次的代碼忘記加上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);
        }


     

    xwork.xml中配置result-type

            <result-types>

                <result-type name="image"

                  class="com.bnt.afp.action.verify.ImageResult"/>

            </result-types>

     

    添加一個生成圖片的action

            <action name="imageAction"

    class="com.bnt.afp.action.verify.ImageAction">

                <result name="success" type="image"/>

            </action>

     

    在需要生成驗證圖片的地方這樣調用:

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


     ImageAction里只要簡單的返回SUCCESS就可以了

        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(
    00, 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);

           }


                                                                                                                                     轉載請注明作者和來源. 

    posted on 2005-11-15 21:14 fisher 閱讀(1663) 評論(2)  編輯  收藏 所屬分類: Programing

    評論

    # 在webwork中使用自定義的Result生成動態驗證圖片[TrackBack]  回復  更多評論   

    在webwork中使用自定義的Result輸出流式的圖片,并通過設置response為不刷新來刷新圖片
    [引用提示]fisher on csdn 引用了該文章, 地址: http://blog.csdn.net/flyingbug/archive/2005/11/19/532783.aspx
    2005-11-19 10:03 | fisher on csdn

    # re: 在webwork中使用自定義的Result生成動態驗證圖片  回復  更多評論   

    直接用webwork的流返回就行了
    2007-11-30 18:03 | kscn
    主站蜘蛛池模板: 亚洲AV无码码潮喷在线观看 | 曰韩亚洲av人人夜夜澡人人爽 | 亚洲综合图色40p| 边摸边脱吃奶边高潮视频免费| 狼人大香伊蕉国产WWW亚洲| 国产日韩精品无码区免费专区国产 | 精品在线观看免费| 222www免费视频| 免费看一级做a爰片久久| 亚洲精品蜜桃久久久久久| 亚洲娇小性色xxxx| 一级做a爱过程免费视频高清| 久久成人a毛片免费观看网站| 99久久这里只精品国产免费| 亚洲欧洲日产国码一级毛片| 亚洲网址在线观看你懂的| 亚洲欧美不卡高清在线| 91在线视频免费观看| 四虎在线视频免费观看| 久久精品国产精品亚洲色婷婷 | 免费国产作爱视频网站| 亚洲国产精品一区二区成人片国内| 亚洲欧美日韩国产精品一区| 日韩免费观看一级毛片看看| 久久久无码精品亚洲日韩按摩| 免费看一级高潮毛片| 四虎成人精品一区二区免费网站 | 亚洲国产精品狼友中文久久久| 亚洲一区二区三区在线网站| 处破痛哭A√18成年片免费| 亚洲AV成人片无码网站| 亚洲成在人线aⅴ免费毛片| 立即播放免费毛片一级| 亚洲国产精品成人精品无码区 | 99热免费在线观看| 亚洲人成网站在线观看播放| 成人嫩草影院免费观看| 亚洲AV无码乱码精品国产| 国产免费一区二区三区在线观看| 亚洲一区二区三区无码中文字幕| 91视频免费网址|