-
Java生成随机验证码
- package com.tg.snail.core.util;
-
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.util.Random;
- import javax.imageio.ImageIO;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import org.apache.log4j.Logger;
- import org.springframework.util.StringUtils;
-
- public abstract class RandomCodeUtils {
-
- private static Logger logger = Logger.getLogger(RandomCodeUtils.class);
-
- private static final String RANDOM_CODE_KEY = "random";
-
- private static final int CODE_NUM = 4;
-
-
- private static Font myFont = new Font("Arial", Font.BOLD, 16);
-
-
- private static char[] charSequence = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
-
- private static Random random = new Random();
-
-
-
- public static void createRandomCode(HttpServletRequest request, HttpServletResponse response){
-
- response.setHeader("Pragma", "No-cache");
- response.setHeader("Cache-Control", "no-cache");
- response.setDateHeader("Expires", 0);
- response.setContentType("image/jpeg");
-
- int width = 80, height = 25;
-
- BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
-
- Graphics g = image.getGraphics();
- g.setColor(getRandomColor(200, 250));
- g.fillRect(1, 1, width - 1, height - 1);
- g.setColor(new Color(102, 102, 102));
- g.drawRect(0, 0, width - 1, height - 1);
- g.setFont(myFont);
-
- g.setColor(getRandomColor(160, 200));
- for (int i = 0; i < 155; i++) {
- int x = random.nextInt(width - 1);
- int y = random.nextInt(height - 1);
- int x1 = random.nextInt(6) + 1;
- int y1 = random.nextInt(12) + 1;
- g.drawLine(x, y, x + x1, y + y1);
- }
-
- for (int i = 0; i < 70; i++) {
- int x = random.nextInt(width - 1);
- int y = random.nextInt(height - 1);
- int x1 = random.nextInt(12) + 1;
- int y1 = random.nextInt(6) + 1;
- g.drawLine(x, y, x - x1, y - y1);
- }
-
-
- StringBuilder sRand = new StringBuilder(CODE_NUM);
- for (int i = 0; i < CODE_NUM; i++) {
-
- String tmp = getRandomChar();
- sRand.append(tmp);
-
- g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
- g.drawString(tmp, 15 * i + 10, 20);
- }
-
-
- HttpSession session = request.getSession(true);
-
- session.setAttribute(RANDOM_CODE_KEY, sRand.toString());
- g.dispose();
-
- try {
- ImageIO.write(image, "JPEG", response.getOutputStream());
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
-
-
- public static boolean checkRandomCode(HttpServletRequest request, String inputCode){
- HttpSession session = request.getSession(false);
- if(session != null && StringUtils.hasLength(inputCode)){
- String code = (String) session.getAttribute(RANDOM_CODE_KEY);
- logger.info("inputCode:"+inputCode.trim()+",code:"+code);
- return inputCode.trim().equalsIgnoreCase(code);
- }
- return false;
- }
-
-
- public static void removeRandomCode(HttpServletRequest request){
- HttpSession session = request.getSession(false);
- if(session != null){
- session.removeAttribute(RANDOM_CODE_KEY);
- }
- }
-
-
- private static Color getRandomColor(int fc, int bc) {
- Random random = new Random();
- if (fc > 255)
- fc = 255;
- if (bc > 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);
- }
-
-
- private static String getRandomChar() {
- int index = random.nextInt(charSequence.length);
- return String.valueOf(charSequence[index]);
- }
- }
-
相关阅读:
强大的mono.cecil
关于svn不能cleanup的问题
SVN项目,快速查看项目的当前版本号
jQuery选择器总结
将Excel数据导入mysql数据库的几种方法
SpringMVC表单标签简介
mybatis动态SQL语句
mysql时间格式化,按时间段查询MYSQL语句
深入了解ios系统机制
Eclipse 官方简体中文语言包下载地址及安装方法
-
原文地址:https://www.cnblogs.com/slimer/p/4740766.html
Copyright © 2020-2023
润新知