Written verification code

package com.hopetesting.web.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
* @author newcityman
* @date 2019/9/1 - 20:42
*/
@WebServlet("/ checkCode")
public class CheckCodeServlet the extends the HttpServlet {
protected void the doPost (the HttpServletRequest Request , the HttpServletResponse Response) throws ServletException , IOException {
//. 1, create an object image stored in memory (CAPTCHA objects) int width = 100 ; height = int 50 ; the BufferedImage image = new new the BufferedImage (width , height , the BufferedImage. TYPE_INT_RGB that) ; // 2, landscape image // 2.1, fill the background color Graphics image.getGraphics G = () ; g.setColor (. color PINK) ; g.fillRect ( 0 , 0 , width







, height) ;

// 2.2, Videos rectangular border
g.setColor (Color. Blue) ;
g.drawRect ( 0 , 0 , width- . 1 , height- . 1) ;

// 2.3, randomly generated subscript
String STR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghighjklmnopqrstuvwxyz0123456789 " ;
the Random RAN = new new the Random () ;

// Get the character
g.setColor (Color. RED) ;
for ( int I = . 1 ; I <= . 4 ; I ++) {
int index = ran.nextInt (str.length () ) ;
char str.charAt = C (index) ;
g.drawString (C + "" , width /* I. 5 , height / 2) ;
}
// set the interference line 2.4
g.setColor (Color. BLACK) ;

for ( int I = 0 ; I < 10 ; I ++) {
int ran.nextInt X1 = (width) ;
int ran.nextInt = X2 (width) ;

int ran.nextInt Y1 = (height) ;
int Y2 = ran.nextInt (height) ;
g.drawLine (X1 , Y1 , X2 , Y2) ;
}


//. 3, the image delivery to the page display
ImageIO's. Write (Image , "JPG" , response.getOutputStream ()) ;


}

protected void the doGet (Request the HttpServletRequest, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}

Guess you like

Origin www.cnblogs.com/newcityboy/p/11443805.html