Simple Verification Code Business

Source: Internet
Author: User

1 //generate a picture response stream, which is returned to the client2         //before it was text/html .3         //A. Set the response type to picture4Response.setcontenttype ("Image/jpeg");5         //B. Creating a Picture object BufferedImage (0, 0, 0): Parameter 1 picture width parameter 2 picture height parameter 3 pixel of picture6BufferedImage image =NewBufferedImage (70, 30, Bufferedimage.type_int_rgb);7Graphics g = image.getgraphics ();//gets the Brush object for the picture object8         //c. Background setting gray background9 G.setcolor (color.gray);TenG.fillrect (0, 0, 70, 30);//Fills the entire picture by filling the rectangle One         //d. Draw a string A         //randomly generate a 5-digit numeric string -Random r =NewRandom (); -         intnum = R.nextint (100000-10000) +10000;//10000~99999 theString code =string.valueof (num);//Convert to String -         //binds the generated string validation to the code attribute in session -HttpSession session =request.getsession (); -Session.setattribute ("Code", code); +          -         //set font color, set font + G.setcolor (color.blue); AG.setfont (NewFont ("Consolas", font.bold,20)); at         //draw a string on a picture object in memory -g.DrawString (Code, 5, 20); -         //draw some lines to increase the difficulty of cracking - G.setcolor (color.black); -G.drawline (R.nextint), R.nextint (+), R.nextint (30), R.nextint ()); -G.drawline (R.nextint), R.nextint (+), R.nextint (30), R.nextint ()); inG.drawline (R.nextint), R.nextint (+), R.nextint (30), R.nextint ()); -G.drawline (R.nextint), R.nextint (+), R.nextint (30), R.nextint ()); to          +         //E. Responding to a page by streaming a picture object -         //write (IM, formatname, output): Im is the picture object FormatName is the format of the picture output is the stream object theImageio.write (Image, "JPEG", Response.getoutputstream ());

Simple Verification Code Business

Related Article

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.