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