日期:2014-05-17 浏览次数:20661 次
package com.free.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.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class CodeMakerServlet extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;
//生成随机类
Random random = new Random();
//验证码字体
private Font[] codeFont =
{
new Font("Times New Roman", Font.PLAIN,18),
new Font("Times New Roman", Font.PLAIN, 18),
new Font("Times New Roman", Font.PLAIN,18),
new Font("Times New Roman", Font.PLAIN, 18)
};
//验证码数字颜色
private Color[] color =
{
Color.BLACK, Color.RED, Color.DARK_GRAY, Color.BLUE
};
String codeNumbers = "";
int width = 60, height = 20;
// 处理 HTTP get 请求
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("生成验证码");
// 清空缓冲区
response.reset();
// 注意这里的MIME类型
response.setContentType("image/png");
// 设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 创建一个图像,验证码显示的图片大小
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
&