Struts2 return verification code

Source: Internet
Author: User

Configure the Struts. xml file

<Constant name ="Struts. Enable. dynamicmethodinvocation"Value ="True"/> Change value to true. When an action is accessed, the action class is called and the execute method is not executed by default.

If it is false, which call Action to execute the execute method?

<Package name ="Default"Namespace ="/"Extends ="Struts-Default"> Namespace: the prefix used to access action extends: It can inherit from other packages. Name: package name

<Action name ="Validatebar"Class ="Net. dbet. Action. validatebaraction" Method="Download">

Name: Action name class: when an action is accessed, an action class method is executed by default to call the methods executed in this class.

<Result name ="Down"Type ="Stream"> The default value returned by the method in the action class called by action is automatically matched with the same result name type: returned as a stream.

The file to be downloaded is in JPG format: <Param name = "contenttype"> image/jpg </param>

<Param name ="Contenttype"> Application/octet-stream </param> downloads the corresponding format file no matter what files are uploaded.

<Param name ="Inputname"> Downloadfile </param> the downloadfile object in the sownload method of net. dbet. Action. fileoploadaction corresponding to downloadfile

</Result>

</Action>

</Package>

 

 

Create a JSP page

<Body>

<Image src ="Validatebar"> </Image> // The path is an action.

</Body>

 

 

Create action class

PackageNet. dbet. Action;

 

ImportJava. Io. fileinputstream;

 

ImportNet. dbet. util. validatebarutil;

 

ImportOrg. Apache. struts2.servletactioncontext;

 

ImportCom. opensymphony. xwork2.actioncontext;

 

Public ClassValidatebaraction {

PrivateInputstream downloadfile; and <Param name ="Inputname"> Downloadfile </param> must be consistent.

PublicInputstream getdownloadfile (){

ReturnDownloadfile;

}

PublicString download (){

Try{

This. Downloadfile = validatebarutil.Createasstream(Actioncontext.Getcontext(). Getsession ());

// CallCreateasstreamMethod to pass the currentSessionSession

}Catch(Exception e ){

E. printstacktrace ();

}

Return"Down ";

}

}

 

Create validatebarutil class

PackageNet. dbet. util;

 

ImportJava. AWT. color;

ImportJava. AWT. Font;

ImportJava. AWT. graphics;

ImportJava. AWT. image. bufferedimage;

ImportJava. Io. bytearrayinputstream;

ImportJava. Io. bytearrayoutputstream;

ImportJava. Io. ioexception;

ImportJava. Io. inputstream;

ImportJava. util. Map;

ImportJava. util. Random;

 

ImportJavax. ImageIO. ImageIO;

 

Public ClassValidatebarutil {

Public StaticInputstream createasstream (MAP session)

{

Return NewBytearrayinputstream (Create(Session ));

}

Public Static Byte[] Create (MAP session) {// create an image in memory

IntWidth = 60, Height = 20; // defines the image width and height.

Bufferedimage image =NewBufferedimage (width, height,

Bufferedimage.Type_int_rgb); // Save the generated image to bufferedimage

 

// Obtain the image Context

Graphics G = image. getgraphics (); // obtain the paint brush

 

// Generate a random class

Random random =NewRandom ();

 

// Set the background color

G. setcolor (Getrandcolor(200,250); // paint brush color

G. fillrect (0, 0, width, height); // draw a rectangle.

 

// Set the font

G. setfont (NewFont ("Times New Roman", Font.Plain, 18 ));

 

// Draw a border

// G. setcolor (new color ());

// G. drawrect (0, 0, width-1, height-1 );

 

// Generates 155 random interference lines, making the authentication code in the image hard to be detected by other programs.

G. setcolor (Getrandcolor(160,200); // set the paint brush color.

For(IntI = 0; I <155; I ++ ){

IntX = random. nextint (width );

IntY = random. nextint (height );

IntXL = random. nextint (12 );

IntYL = random. nextint (12 );

G. drawline (X, Y, x + XL, Y + yl );

}

 

// Obtain the random ID code (4 digits)

String srand = "";

For(IntI = 0; I <4; I ++ ){

String Rand = string.Valueof(Random. nextint (10 ));

Srand + = rand;

// Display the authentication code to the image

G. setcolor (NewColor (20 + random. nextint (110), 20 + random

. Nextint (110), 20 + random. nextint (110); // the color of the called function is the same. It may be because the seed is too close, so it can only be generated directly.

G. drawstring (RAND, 13 * I + 6, 16 );

}

 

// Save the authentication code to the session

Session. Put ("validatebar", srand );

 

// The image takes effect

G. Dispose ();

// Output the image to the page

Bytearrayoutputstream baos =NewBytearrayoutputstream (); // create a byte array

Try{

ImageIO.Write(Image, "Jpeg", baos); // write the drawn image to the byte array.

}Catch(Ioexception e ){

E. printstacktrace ();

}

ReturnBaos. tobytearray ();

}

 

Public StaticColor getrandcolor (IntFC,IntBC) {// obtain a random color from a given range

Random random =NewRandom ();

If(FC> 255)

Fc = 255;

If(Bc> 255)

BC = 255;

IntR = FC + random. nextint (BC-Fc );

IntG = FC + random. nextint (BC-Fc );

IntB = FC + random. nextint (BC-Fc );

Return NewColor (R, G, B );

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.