Generar un paquete de código de verificación aleatorio

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.io.OutputStream; 
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; 

import org.apache.commons.lang3.math.NumberUtils; 
import org.apache.commons.lang3.StringUtils; 

/ ** 
 * 生成 随机 验证 码
 * @author linxiaoqing 
 * 
 * / 
@SuppressWarnings ("serial")
public class ValidateCodeServlet extiende HttpServlet { 
	
	public static final String VALIDATE_CODE = "validateCode"; 
	
	int privado w = 70; 
	int privado h = 26; 
	
	public ValidateCodeServlet () { 
		super (); 
	} 
	
	public void destroy () { 
		super.destroy (); 
	} 
	
	validación booleana estática pública (solicitud HttpServletRequest, String validateCode) { 
		String code = (String) request.getSession (). getAttribute (VALIDATE_CODE); 
		return validateCode.toUpperCase (). equals (código); 
	} 

	public void doGet (solicitud HttpServletRequest, respuesta HttpServletResponse) 
			arroja ServletException, IOException {
		Cadena validateCode = request.getParameter (VALIDATE_CODE); // AJAX , , 成功 返回 true  
		if (StringUtils.isNotBlank (validateCode)) {
			response.getOutputStream (). Print (validate (request, validateCode)? "True": "false"); 
		} else { 
			this.doPost (solicitud, respuesta); 
		} 
	} 

	public void doPost (solicitud HttpServletRequest, respuesta HttpServletResponse) 
			arroja ServletException, IOException { 
		createImage (solicitud, respuesta); 
	} 
	
	private void createImage (solicitud HttpServletRequest, 
			respuesta HttpServletResponse) arroja IOException { 
		
		response.setHeader ("Pragma", "no-cache"); 
		response.setHeader ("Cache-Control", "no-cache"); 
		response.setDateHeader ("Expira", 0);
		response.setContentType ("image / jpeg"); 
		
		/ *
		g.dispose (); 
		 * 得到 参数 高 , 宽 , 都 为 数字 时 , 则 使用 设置 高 宽 , 否则 使用 默认 值
		 * /
		Ancho de cadena = request.getParameter ("ancho"); 
		Altura de cadena = request.getParameter ("altura"); 
		if (StringUtils.isNumeric (ancho) && StringUtils.isNumeric (alto)) { 
			w = NumberUtils.toInt (ancho); 
			h = NumberUtils.toInt (altura); 
		} 
		
		Imagen BufferedImage = nueva BufferedImage (w, h, BufferedImage.TYPE_INT_RGB); 
		Gráficos g = image.getGraphics (); 

		/ * 
		 * 生成 背景
		 * / 
		createBackground (g); 

		/ * 
		 * 生成 字符
		 * / 
		Cadena s = createCharacter (g); 
		request.getSession (). setAttribute (VALIDATE_CODE, s); 

		OutputStream out = response.getOutputStream (); 
		ImageIO.write (imagen, "JPEG", fuera);
		out.close (); 

	} 
	
	color privado getRandColor (int fc, int bc) { 
		int f = fc; 
		int b = bc; 
		Aleatorio aleatorio = nuevo Aleatorio (); 
        si (f> 255) { 
        	f = 255; 
        } 
        if (b> 255) { 
        	b = 255; 
        } 
        devuelve nuevo Color (f + random.nextInt (bf), f + random.nextInt (bf), f + random.nextInt (bf)); 
	} 
	
	vacío privado createBackground (Gráficos g) { 
		// 填充 背景
		g.setColor (getRandColor (220,250)); 
		g.fillRect (0, 0, w, h); 
		// 加入 干扰 线条
		para (int i = 0; i <8; i ++) { 
			g.setColor (getRandColor (40,150)); 
			Random random = new Random (); 
			int x = random.nextInt (w);
			int y = random.nextInt (h); 
			int x1 = random.nextInt (w); 
			int y1 = random.nextInt (h); 
			g.drawLine (x, y, x1, y1); 
		} 
	} 

	cadena privada createCharacter (Gráficos g) { 
		char [] codeSeq = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J ', 
				' K ',' M ',' N ',' P ',' Q ',' R ',' S ',' T ',' U ',' V ',' W ', 
				' X ', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9'}; 
		Cadena [] fontTypes = {"Arial", "Arial Black", "AvantGarde Bk BT", "Calibri"}; 
		Aleatorio aleatorio = nuevo Aleatorio (); 
		StringBuilder s = new StringBuilder (); 
			Cadena r = String.valueOf (codeSeq [random.nextInt (codeSeq.length)]); // random.nextInt (10));
			g.setColor (nuevo Color (50 + random.nextInt (100), 50 + random.nextInt (100), 50 + random.nextInt (100))); 
			g.setFont (nueva fuente (fontTypes [random.nextInt (fontTypes.length)], Font.BOLD, 26)); 
			g.drawString (r, 15 * i + 5, 19 + random.nextInt (8)); 
// g.drawString (r, i * w / 4, h-5); 
			s.append (r); 
		} 
		return s.toString (); 
	} 
	
}

  

Supongo que te gusta

Origin www.cnblogs.com/Koaler/p/12677567.html
Recomendado
Clasificación