Joke Collection Website - Public benefit messages - Java swing random verification code
Java swing random verification code
HttpServletResponse? Response)? Throwing? ServletException,? IOException? {
?
? response . setcontenttype(" image/JPEG ");
? Response.setHeader("Pragma ","no cache ");
? Response.setheader ("cache-control", "no cache");
? response . setdate header(" Expires ",0);
? HttpSession? Conversation? =? request . getsession();
?
? int? Width? =? 60,? Height? =? 20;
?
? BufferedImage? Image? =? New? BufferedImage (width, height,
BufferedImage。 TYPE _ INT _ RGB);
?
? //? Get graphic context
? Graphics? g? =? image . get graphics();
?
? //? Generate random class
? Random? Random? =? New? random();
?
? //? Set the background color.
? g.setColor(getRandColor(200,? 250));
? g.fillRect(0,? 0,? Width? Height);
?
? //? Set font
? G.setFont (new? Font("Times? New? Roman? Font. Flat and faint? 18));
?
? //? 155 interference line is generated randomly, which makes the authentication code in the image difficult to be detected by other programs.
? g . set color(getRandColor( 160,? 200));
? For what? (int? Me? =? 0; ? Me? & lt? 155; ? i++)? {
int? x? =? Random.nextInt (width);
int? y? =? Random.nextInt (height);
int? xl? =? random . nextint( 12);
int? yl? =? random . nextint( 12);
g.drawLine(x,y,? x? +? xl,? y? +? yl);
? }
?
? //? Take the randomly generated authentication code (4 digits)
? String? sRand? =? "";
? For what? (int? Me? =? 0; ? Me? & lt? 4; ? i++)? {
String? Rand? =? string . value of(random . nextint( 10));
sRand? +=? Rand;
//? Display the verification code to the image.
G.setColor (new? Color (20? +? random.nextInt( 1 10),? 20? +? at will
? . nextInt( 1 10),? 20? +? random . nextint( 1 10))); //? Call functions are all the same color, probably because the seeds are too close, so they can only be generated directly.
G. draw rope (rand, 13? *? Me? +? 6,? 16);
? }
?
? //? Store the authentication code in the session.
? session.setAttribute("rand ",sRand);
? //? Image verification
? g . dispose();
? ServletOutputStream? responseOutputStream? =? response . get output stream();
? //? Output the image to the page
? ImageIO.write(image,? “JPEG”,? responseOutputStream);
?
? //? Close the input stream below!
? responseoutputstream . flush();
? responseoutputstream . close();
? }
?
Static electricity Color? getRandColor(int? fc,? int? bc)? {
? //? Gets a random color in a given range.
? Random? Random? =? New? random();
? What if? (fc? & gt? 255)
fc? =? 255;
? What if? (BC? & gt? 255)
BC? =? 255;
? int? r? =? fc? +? random.nextInt(bc? -? fc);
? int? g? =? fc? +? random.nextInt(bc? -? fc);
? int? b? =? fc? +? random.nextInt(bc? -? fc);
? Return? New? Color (r, g,? b);
? }
?
? /**?
? *? Handle? That? HTTP? Get? Method. ?
? *
? *? @param? Request?
? *servlet? Request?
? *? @param? Response?
? *servlet? Response?
? */?
? Protected? Invalid? doGet(HttpServletRequest? Request,
HttpServletResponse? Response)? Throwing? ServletException,? IOException? {
? ProcessRequest (request, response);
? } This is the verification code used in my web. You can change it.
- Previous article:How to close WeChat applet
- Next article:What channels can BOC individual customers register for SMS notification?
- Related articles
- Received SMS of pollution risk of express delivery.
- How can I inform my relatives of the housewarming wine?
- How to delete contacts who often send text messages?
- Birthday wishes for the opposite sex with high emotional intelligence
- How to cancel the SMS signature of 139 mailbox?
- How to set the ringtone of mobile phone
- Does an enterprise bank need a legal person to add collection information?
- Will credit card installment be reminded by SMS?
- Why can't I print my invoice? Where is the SMS verification code? There is no place to start.
- 100 words resignation greetings to colleagues _ resignation greetings to colleagues