免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
在webwork中使用自定義的Result生成動(dòng)態(tài)驗(yàn)證圖片

在webwork中使用自定義的Result生成動(dòng)態(tài)驗(yàn)證圖片

這個(gè)動(dòng)態(tài)圖片的實(shí)現(xiàn)原理是在servlet的response中寫(xiě)入一個(gè)ImageOutputStream,并由servlet容器將其轉(zhuǎn)成圖片,在非webwork的實(shí)現(xiàn)中,可以直接操作response,但是在webwork中,要想直接操作response的output則必須使用不需要對(duì)response操作的result類型

實(shí)現(xiàn)一個(gè)
Result

 

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

上次的代碼忘記加上response的設(shè)置不緩存了,這樣即使使用IE的回退也會(huì)刷新圖片 

    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>

 

添加一個(gè)生成圖片的action

        <action name="imageAction"

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

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

        </action>

 

在需要生成驗(yàn)證圖片的地方這樣調(diào)用:

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


 ImageAction里只要簡(jiǎn)單的返回SUCCESS就可以了

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



VerifyImage中生成圖片的方法:(來(lái)自網(wǎng)上一個(gè)JSP生成動(dòng)態(tài)驗(yàn)證圖片的實(shí)例)

       //獲取生成的圖片,返回生成的驗(yàn)證碼,并將ImageOutputStream寫(xiě)入

       
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);

       }
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
jsp驗(yàn)證碼
java生成圖片驗(yàn)證碼
JSP彩色驗(yàn)證碼
登陸注冊(cè)使用的驗(yàn)證碼(java生成)
Servlet 實(shí)現(xiàn)隨機(jī)驗(yàn)證碼
導(dǎo)出文件
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服