[Picture.java]
Package creator.common.attachlogin;
Import java.io. *;
Public class picture {
Final Int key = 1;
Final string error_format_int = "format of color is not rgb.sample /" 212 | 232 | 0 / ""
Final string error_color_input = "format of color (num): Num in 0-255
/ ** Format Output Data ** /
Public string management (String Temp) {
String Returnstr = ""
Temp = Encrypt (TEMP);
Byte [] by = TEMP.GETBYTES ();
For (int i = 0; i Returnstr = Returnstr (Byte) by [i] "|"; } Return Returnstr; } / ** Format Input Data ** / Public Byte [] Dismanage (String Temp) { INT LEN = 0, INDEX = 0, i = 0, first = 0; While ((i = temp.indexof ("|", first))> - 1) { Len ; FigSt = i 1; } Byte [] by = new byte [len]; FigSt = 0; While ((i = temp.indexof ("|", first))> - 1) { By [index] = byte.Parsebyte (Temp.Substring (First, i)); INDEX ; FigSt = i 1; } Return by; } / ** randomly generate four additional code ** / Public String getrandom () { INT I1 = (int) (java.lang.math.random () * 10); INT I2 = (int) (java.lang.math.random () * 10); INT i3 = (int) (java.lang.math.random () * 10); INT i4 = (int) (java.lang.math.random () * 10); Return String.Valueof (i1) String.Valueof (i2) String.Valueof (i3) String.Valueof (i4); } / ** Encryption 1: misalignment processing ** / Public String Encrypt (String Randomstr) { String para = random () randomstr.substring (0, 1) random () random () randomstr.substring (1, 2); Para = Para Random () randomstr.substring (2); Return Jiami (Para); } / ** Get a random number 0-9 ** / Private string random () { String Temp = String.Valueof ((int) (java.lang.math.random () * 10)); Return Temp; } / ** Encryption 2: Encryption processing, this method can modify ** / private string jiami (String Str) { Byte [] by = str.getbytes (); BYTEARRAYINPUTSTREAM IN = New ByteArrayInputStream (by); int CH; INT INDEX = 0; Byte [] temp = new byte [in.available ()]; While (chile (ch = in.read ())! = - 1) { Temp [index] = (BYTE) (ch-key); INDEX ; } BYTEARRAYINPUTSTREAM INS = New ByteArrayInputStream (TEMP); BufferedReader Freader = New BufferedReader (inPutStreamReader); Try {returnFReader.readLine ();} catch (exception e) {return "; } / ** Get the correct number from the number ** / Public String Discrypt (String Temp) { String Para = Jiemi (Dismanage (TEMP)); Return Para.Substring (1, 2) Para.Substring (4, 5) Para.Substring (6, 8); } / ** Decryption handling ** / Private string jiemi (byte [] by) { BYTEARRAYINPUTSTREAM IN = New ByteArrayInputStream (by); int CH; INT INDEX = 0; Byte [] temp = new byte [in.available ()]; While (chile (ch = in.read ())! = - 1) { Temp [Index] = (Byte) (CH Key); INDEX ; } BYTEARRAYINPUTSTREAM INS = New ByteArrayInputStream (TEMP); BufferedReader Freader = New BufferedReader (inPutStreamReader (INS); Try {returnFReader.readLine ();} catch (exception e) {return "; } / ** Decompose color Num | NUM | NUM ** / Public int [] masterdata (string temp) { INDEX_LEN = 0, INDEX = 0, Next_index = 0; int [] return_arr = new int rt [3]; Boolean Break_ERROR = false; IF (GetMax (Temp, "|") == 2) { While ((Index_len = Temp.indexof ("|", next_index)> - 1) { IF (getInt (Temp.substring (next_index, index_len) == 256) { Break_ERROR = true; } else { Return_arr [index] = getInt (Temp.substring (next_index, index_len); Next_index = INDEX_LEN 1; INDEX ; } IF (BREAK_ERROR) BREAK;} IF (BREAK_ERROR) { Return NULL; } else { Return_arr [index] = getInt (Temp.substring (next_index)); RETURN RETURN_ARR; } } else { System.out.println (ERROR_FORMAT_INT ":" TEMP); Return NULL; } } Private int getmax (string temp, string temp2) { INDEX = 0, INDEX_LEN = 0, INDEX_NEXT = 0; While ((Index = Temp.indexof (Temp2, Index_Next)> - 1) { INDEX_LEN ; INDEX_NEXT = INDEX 1; } Return Index_Len; } Private int getint (String Temp) { Try { Return Integer.Parseint (TEMP); } catch (exception e) { System.out.println (ERROR_COLOR_INPUT ":" TEMP); Return 256; } } } [CreateImage.java] Package creator.common.attachlogin; Import java.io. *; Import javax.servlet. *; Import javax.servlet.http. *; Import java.awt. *; Import java.awt.image. *; Import java.awt.image.bufferedImage; Import com.sun.Image.codec.jpeg. *; Import com.sun.jpegcodec; Public class createImage extends httpservlet { Static Final Private string content_type = "text / html; charSet = GB2312"; Final String Input_back_color_error = "Input RGB Backcolor IS Error"; Final string infut_fore_color_error = "Input RGB Forecolor is Error"; Private PICTURE PIC = New Picture (); // Initialize Global Variables Public void init () throws servletexception { } // Process the http get request Public void doget (httpservletRequest request, httpservletResponse response) throws servletexception, ioException {// generated picture try {int imagewidth = 60; int imageheight = 20; BufferedImage image = new BufferedImage (imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); Graphics graphics = image.getGraphics (); graphics.setColor (Color.white); graphics.fillRect (0,0, imageWidth, imageHeight); graphics.setColor ( Color.white); FileOutputStream fos = new FileOutputStream ( "attach.jpg"); BufferedOutputStream bos = new BufferedOutputStream (fos); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (bos); encoder.encode (image); bos.close (); } catCH (Exception E) {system.out.println ("Generate Picture Error!"); String querynum = Request.getParameter ("image"); String queryRGB = ""; IF (Request.GetParameter ("RGB")! = null) { QueryRGB = Request.getParameter ("RGB"); } Response.setHeader ("Cache-Control", "No-Store"); Response.setContentType ("Image / JPEG"); ServletOutputStream out = response.getOutputStream (); // jpg format background color picture (same as the page style), width 3.6 mm, high 1.8 mm InputStream Imagein = New FileInputStream (New File ("attach.jpg")); JPEGIMAGEDECODER DECODER = JPEGCODEC.CREATEJPEGDECODER (Imagein); Bufferedimage image4 = decoder.decodeasbufferedImage (); Querynum = pic.discrypt (querynum); Graphics g = image4.getgraphics (); Queryrgb.Length ()> 1) { IF (pic.masterdata (queryrgb)! = null) { Int [] arg = pic.masterdata (queryRGB); G.SetColor (New Color (Arg [0], Arg [1], Arg [2])); } } else { G.SetColor (New Color (255, 0, 0)); } g.drawstring (querynum, 0, 13); JPEGIMAGEENCODER ENCODER = JPEGCODEC.CREATEJPEGENCODER (OUT); Encoder.Encode (image4); Out.close (); } } [Web.xml] <% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "javabean.com.picture"%> <% PICTURE PIC = New Picture (); string random = pic.getrandom () String Encryrandom = Pic.Manage (Random);%> Additional code: p> div> body> html>