Generate Verification Code summary and generate Verification Code
Java verification code generation Summary
1. serialVersionUID
Private static final long serialVersionUID =-8501285780349046114L;
Java serialization mechanism verifies version consistency by determining the serialVersionUID of the class at runtime. ID card equivalent to a java class
. It is mainly used for version control.
2. BufferedImage class
-- BufferedImage sub-class describes the Image with an accessible Image data buffer.
TYPE_INT_RGB
Represents an image, which has 8-bit RGB color components that synthesize integer pixels.
-- GetGraphics ()
This method returns Graphics2D, but this is for backward compatibility considerations.
3. request. getParameter ()
The data transmitted by the request. getParameter () method is transmitted from the Web Client to the Web server, representing the HTTP request data.
The request. getParameter () method returns String-type data.
4. String... excludeProperty indicates an indefinite parameter, that is, multiple String objects can be passed in when this method is called. (Variable Parameter: this parameter is applicable when the number of parameters is uncertain and the type is determined, java treats variable parameters as arrays. Note: The variable parameter must be located in the last eg: private String drawRandomNum (Graphics2D g, String... createTypeFlag ))
5. Graphics
The Graphics class is an abstract base class for all graphic contexts. It allows applications to draw on components (implemented on various devices) and closed screen images.
[STEP ]:
1. Create an image in the memory;
BufferedImage bi = new BufferedImage (WIDTH, HEIGHT, BufferedImage. TYPE_INT_RGB );
-- BufferedImage sub-class describes the Image with an accessible Image data buffer.
TYPE_INT_RGB represents an image, which has 8-bit RGB color components that synthesize integer pixels.
-- GetGraphics ()
This method returns Graphics2D, but this is for backward compatibility considerations.
2. Obtain the image;
Graphics g = bi. getGraphics ();
3. Set the background color of the image;
SetBackGround (g );
4. Set the border of the image;
SetBorder (g );
5. Draw interference lines on images;
DrawRandomLine (g );
6. Random numbers written on Images;
String random = drawRandomNum (Graphics2D) g, createTypeFlag); // The createTypeFlag passed by the client
7. Store the random number in the session;
Request. getSession (). setAttribute ("checkcode", random );
8. Set the response header to notify the browser to open it in the form of images;
Response. setContentType ("image/jpeg"); // equivalent to res [onse. setHeader ("Content-Type", "image/jpeg ");
9. Set the response header to control the browser against caching;
Response. setDateHeader ("expries",-1 );
Response. setHeader ("Cache-Control", "no-cache ");
Response. setHeader ("Pragma", "no-cache ");
-- SetDateHeader
Public void setDateHeader (java. lang. String name, long date)
Parameters:
Name-the name of the header to set
Date-the assigned date value
-- SetHeader
Public void setHeader (java. lang. String name, java. lang. String value)
Parameters:
Name-the name of the header
Value-the header value If it contains octet string, it shocould be encoded according to RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt)
10. Write images to the browser;
ImageIO. write (bi, "jpg", response. getOutputStream ());
-- Parameter:
Im-The RenderedImage to be written.
FormatName-String containing informal names.
Output-The OutputStream in which data is written.
Throw:
IllegalArgumentException-if any parameter is null.
IOException-if an error occurs during the write process.
Source code reference http://blog.csdn.net/qq_23052951/article/details/50372550