It is important to note that the second approach should be used under resin:
<% @pageImport= "java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*, Sun.misc.BASE64Encoder, Java.awt.image.DataBufferByte , Java.io.*,javax.swing.imageicon "%>//Tomcat under<%@ page contenttype= "image/jpg" language= "java"%>//Resin under<%@ page contenttype= "text/html" language= "java"%><%!Color Getrandcolor (intFcintBC) {//get random colors for a given rangeRandom 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);}%><%out.clear ();//Settings page does not cacheResponse.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-cache"); Response.setdateheader ("Expires", 0);//Create an image in memoryintWIDTH=60, height=30; BufferedImage Image=Newbufferedimage (width, height, bufferedimage.type_int_rgb);//Get the graphics contextGraphics g =image.getgraphics ();//Generating Random classesRandom random =NewRandom ();//Set Background colorG.setcolor (Getrandcolor (200,250)); G.fillrect (0, 0, width, height);//Set FontG.setfont (NewFont ("Times New Roman", Font.plain, 32));//Draw Border//G.setcolor (New Color ());//G.drawrect (0,0,width-1,height-1);//randomly generates 155 lines of interference so that the authentication code in the image is not easily detected by other programsG.setcolor (Getrandcolor (160,200)); 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);}//randomly generated authentication code (4 digits)/*for (int i=0; i < srand.length (); i++) {String rand=string.valueof (Srand.charat (i)); Display the authentication code to the image in G.setcolor (new Color (20+random.nextint), 20+random.nextint (20+random.nextint (110)));// The calling function comes out with the same color, possibly because the seed is too close, so only g.drawstring (rand, 13*i + 6, +) can be generated directly;}*/String SRand= ""; for(inti=0;i<4;i++) {String rand=string.valueof (Random.nextint (10)); SRand+=Rand; //Display the authentication code to the imageG.setcolor (NewColor (20+random.nextint, 20+random.nextint (+), 20+random.nextint (110)));//The call function comes out with the same color, possibly because the seed is too close, so it can only be generated directlyg.DrawString (Rand, 13*i+2, 24);}//Save authentication Code to sessionSession.setattribute ("Validatecode", SRand);//Image Effectiveg.dispose ();//use the following method to output image to page under Tomcat//Imageio.write (image, "JPG", Response.getoutputstream ());//base64 solution under resinBytearrayoutputstream byteout =NewBytearrayoutputstream ();Try{imageio.write (image,"JPEG", byteout); byte[] data =Byteout.tobytearray (); Base64encoder Encoder=NewBase64encoder (); Out.write (Encoder.encode (data)); Out.flush (); Out.clear ();} Catch(IOException e) {e.printstacktrace ();}%>