生成图片验证码
public class ValidationCode extends HttpServlet{
private static String codeChars = "01234566789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
/**
* 返回一个随机颜色(color 对象)
* @param minColor
* @param maxColor
* @return
*/
private static Color getRandomColor(int minColor,int maxColor){
Random random = new Random();
if(minColor>255)
minColor = 255;
if(maxColor>255)
minColor = 255;
int red = minColor+random.nextInt(maxColor - minColor);
int green = minColor+random.nextInt(maxColor - minColor);
int blue = minColor+random.nextInt(maxColor - minColor);
return new Color(red, green, blue);
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int charsLength = codeChars.length();
Random random = new Random();
// (三种方式)关闭客户端浏览器缓存
response.setHeader("ragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expiress", 0);
//生成图片验证码
BufferedImage img = new BufferedImage(90,20,BufferedImage.TYPE_INT_BGR);
Graphics g = img.getGraphics();
g.setColor(getRandomColor(180, 250)); //随机设置要填充的颜色
g.fillRect(0, 0, 90, 20);
g.setFont(new Font("Times New Roman",Font.ITALIC,20));
g.setColor(getRandomColor(120, 180)); //随机设置字体的颜色
StringBuilder validationCode = new StringBuilder();
String [] fontNames = {"Times New Roman","Book antiqua" ,"Arial"}; //验证码的随机字体
//随机生成3--5 个验证码
for(int i=0;i<3+random.nextInt(3);i++){
//validationCode
g.setFont(new Font(fontNames[random.nextInt(3)],Font.ITALIC,20));
char codeChar = codeChars.charAt(random.nextInt(charsLength));
validationCode.append(codeChar);
g.setColor(getRandomColor(10, 100));
g.drawString(String.valueOf(codeChar), 16*i+random.nextInt(7), 20-random.nextInt(6));
}
HttpSession session = request.getSession();
// 设置session对象5 分钟失效
session.setMaxInactiveInterval(5*60);
session.setAttribute("validation_code", validationCode);
g.dispose();
OutputStream toClient = response.getOutputStream();
ImageIO.write(img, "png", toClient) ;
}
}
private static String codeChars = "01234566789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
/**
* 返回一个随机颜色(color 对象)
* @param minColor
* @param maxColor
* @return
*/
private static Color getRandomColor(int minColor,int maxColor){
Random random = new Random();
if(minColor>255)
minColor = 255;
if(maxColor>255)
minColor = 255;
int red = minColor+random.nextInt(maxColor - minColor);
int green = minColor+random.nextInt(maxColor - minColor);
int blue = minColor+random.nextInt(maxColor - minColor);
return new Color(red, green, blue);
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int charsLength = codeChars.length();
Random random = new Random();
// (三种方式)关闭客户端浏览器缓存
response.setHeader("ragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expiress", 0);
//生成图片验证码
BufferedImage img = new BufferedImage(90,20,BufferedImage.TYPE_INT_BGR);
Graphics g = img.getGraphics();
g.setColor(getRandomColor(180, 250)); //随机设置要填充的颜色
g.fillRect(0, 0, 90, 20);
g.setFont(new Font("Times New Roman",Font.ITALIC,20));
g.setColor(getRandomColor(120, 180)); //随机设置字体的颜色
StringBuilder validationCode = new StringBuilder();
String [] fontNames = {"Times New Roman","Book antiqua" ,"Arial"}; //验证码的随机字体
//随机生成3--5 个验证码
for(int i=0;i<3+random.nextInt(3);i++){
//validationCode
g.setFont(new Font(fontNames[random.nextInt(3)],Font.ITALIC,20));
char codeChar = codeChars.charAt(random.nextInt(charsLength));
validationCode.append(codeChar);
g.setColor(getRandomColor(10, 100));
g.drawString(String.valueOf(codeChar), 16*i+random.nextInt(7), 20-random.nextInt(6));
}
HttpSession session = request.getSession();
// 设置session对象5 分钟失效
session.setMaxInactiveInterval(5*60);
session.setAttribute("validation_code", validationCode);
g.dispose();
OutputStream toClient = response.getOutputStream();
ImageIO.write(img, "png", toClient) ;
}
}