<1> Verification code verification code core class validatecodeaction
Package com.tar ENA. common. action; import Java. AWT. *; import Java. AWT. image. bufferedimage; import Java. io. bytearrayinputstream; import Java. io. bytearrayoutputstream; import Java. util. random; import javax. imageIO. imageIO; import javax. imageIO. stream. imageoutputstream; import COM. opensymphony. xwork2.actioncontext;/*** implement the verification code on the webpage * @ author jimmyzhang * @ since 2012.5.10 **/public class validatecodeaction {private bytearrayinputstream inputstream; // default execution method Public String execute () throws exception {random r = new random (); // bufferedimage is equivalent to the image cached in memory bufferedimage image = new bufferedimage (60, // The length of the drawing area is 20. // The height of the drawing area is bufferedimage. type_int_rgb); // obtain the drawing tool graphiscsgraphics G = image. getgraphics (); // set the drawing color G. setcolor (new color (R. nextint (1, 255), R. nextint (1, 255), R. nextint (255); // fill the drawing area g from the origin (0, 0. fillrect (0, 0, 60, 20); // generates a random string (5 digits) string number = string. valueof (R. nextint (99999); // draw the number in the image G. setcolor (new color (0, 0); G. drawstring (number, 5, 15); // Save the number in the session. actioncontext AC = actioncontext. getcontext (); AC. getsession (). put ("vcode", number); // draw interference line for (INT I = 0; I <3; I ++) {drawline (G, R );} // bytearrayoutputstream output = new bytearrayoutputstream (); imageoutputstream imageoutput = ImageIO. createimageoutputstream (output); // write the image into imageoutput ImageIO. write (image, "Jpeg", imageoutput); // construct inputstream = new bytearrayinputstream (output. tobytearray (); // conversion between objects -- image --> bytearrayoutstream --> bytearrayinputstream return "success";} // auxiliary method, used to draw an interference line Private void drawline (Graphics g, random R) {G. setcolor (new color (R. nextint (1, 255), R. nextint (1, 255), R. nextint (255); // drawline (x1, Y1, X2, Y2) g. drawline (R. nextint (60), R. nextint (20), R. nextint (60), R. nextint (20);} public bytearrayinputstream getinputstream () {return inputstream;} public void setinputstream (bytearrayinputstream inputstream) {This. inputstream = inputstream ;}}
<2> logic Implementation of registering the verification code in registeraction in struts2.0
Package com.tar ENA. dang. user. action; import Java. io. *; import Org. apache. struts2.servletactioncontext; import COM. opensymphony. xwork2.actioncontext; import com.tar ENA. dang. domain. user; import com.tar ENA. dang. user. service. userservice; import com.tar ENA. dang. user. service. userserviceimpl;/*** register the action controller service on the page * @ author jimmyzhang **/public class registeraction {private userservice; // simplify the use of Entity objects Ing with form elements private user; private string vcode; // defines the property private file upload that won the bet with the file domain upload; // uploads the file private string uploadfilename; // file name to be uploaded private string uploadcontenttype; // Public String getvcode () {return vcode;} public void setvcode (string vcode) {This. vcode = vcode;} public user getuser () {return user;} public void setuser (User user) {This. user = user;} public registeraction () {userservice = new US Erserviceimpl (); system. out. println ("[registeraction] Call the Default Construction Method without Parameters");} Public String execute () throws exception {// verify whether the user input is consistent with the actioncontext AC = actioncontext in the session. getcontext (); string session_vocode = (string) AC. getsession (). get ("vcode"); If (! Vcode. equals (session_vocode) {AC. put ("vcode_info", " The Verification Code does not match"); Return "input";} Boolean issuccess = userservice. register (User); If (issuccess) {// upload the Avatar, File Upload -->/images/user/username.jpg // obtain the server physical path string Path = servletactioncontext. getservletcontext (). getrealpath ("/images/user/" + User. getUserName () + ". jpg "); // create a file object for the Server File: file DEST = new file (PATH); // upload --> dest Inputstream FCM = new fileinputstream (upload); outputstream Fos = new fileoutputstream (DEST); byte [] B = new byte [1024]; int N; while (n = FCM. read (B, 0, B. length ))! =-1) {FOS. write (B, 0, n);} FOS. close (); FCM. close (); // AC. put ("user", user); Return "OK";} else {return "input" ;}} public file getupload () {return upload ;} public void setupload (File Upload) {This. upload = upload;} Public String getuploadfilename () {return uploadfilename;} public void setuploadfilename (string uploadfilename) {This. uploadfilename = uploadfilename;} Public String getuploadcontenttype () {return uploadcontenttype;} public void setuploadcontenttype (string uploadcontenttype) {This. uploadcontenttype = uploadcontenttype ;}}
<3> JSP page for testing the Verification Code Effect
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML>
After finishing the work, you can directly import the source code to the project for testing. What are you waiting?