Package Com.su.servlet;import Java.awt.color;import Java.awt.font;import java.awt.graphics;import Java.awt.graphics2d;import Java.awt.image.bufferedimage;import Java.io.ioexception;import java.util.ArrayList; Import Java.util.list;import java.util.random;import javax.imageio.imageio;import javax.servlet.ServletException; Import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Checkcode extends httpservlet{/** * */private static final long Seri Alversionuid = 1l;public static final int width = 120;//The width of the generated picture public static final int height = 30;//the height of the resulting picture @overrideprote CTED void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {doPost (Request, response);} @Overrideprotected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {request.setcharacterencoding ("utf-8"); Create a picture in memory bufferedimage bi = NEW bufferedimage (WIDTH, HEIGHT, Bufferedimage.type_int_rgb); Get picture Graphics graphics = Bi.getgraphics (); Set the background color of the picture Graphics.setcolor (Color.White); graphics.fillrect (0, 0, width, width);//Set the border of the picture Graphics.setcolor ( color.red); Graphics.drawrect (1, 1, WIDTH-2, HEIGHT-2); Color[] Color={color.black,color.blue,color.cyan,color.green,//define the color array color.orange,color.yellow,color.red, color.pink,color.light_gray};//picture of the interference line Graphics.setcolor (color.blue);//Set the number of lines and draw lines for (int i=0;i<10;i++) { Graphics.setcolor (Color[new random (). Nextint (9)]); int x1 = new Random (). Nextint (WIDTH); int x2 = new Random (). Nextint ( WIDTH); int y1 = new Random (). Nextint (height); int y2 = new Random (). Nextint (HEIGHT); Graphics.drawline (x1, y1, x2, y2);} StringBuilder sb = new StringBuilder ();//write the random number on the image Graphics.setcolor (Color.gray); Graphics.setfont ("Italic", Font.Bold, 20)); Graphics2D g = (graphics2d) graphics; Control word String str = "0123456789ABCDEFGHIGKLMNOPQRSTUVWRST"; int x = 5; for (int i = 0; i < 4; i++) { Set font rotation angle int degree = new Random (). Nextint ()% 30; char ch = str.charat (new Random (). Nextint (36)); Sb.append (string.valueof (ch)); Positive angle g.rotate (degree * math.pi/180, X, 20); Graphics.DrawString (string.valueof (CH), X, 20); Reverse Angle g.rotate (-degree * math.pi/180, X, 20); x + = 30; }//7. The random number exists in the session request.getsession (). SetAttribute ("Checkcode", sb.tostring ());//8. Set the response header to notify the browser to open Response.setcontenttype ("Image/jpeg") as a picture;//9. Setting the response header control browser do not cache Response.setdateheader ("Expries",-1) ; Response.setheader ("Cache-control", "No-cache"), Response.setheader ("Pragma", "No-cache");//10. Write the picture to the browser imageio.write (bi, "JPG", Response.getoutputstream ());}}
Java Verification Code Picture