package com.iamberry.wechat.handles.verify;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.utils.VerifyCodeUtil;
/**
 * @company	深圳爱贝源科技有限公司
 * @website www.iamberry.com
 * @author 	献
 * @tel		18271840547
 * @date	2016年11月3日
 * @explain	注意，以后若需要产生其他验证码，也需要已image为后缀，勿以jpg、png、gif等等为后缀,防止被过滤器拦截（缓存客户端）
 */
@WebServlet("/verify.image")
public class VerifyHander extends HttpServlet {
	private static final long serialVersionUID = 1L;
	/**
	 * 获取验证码,文本会保存到session中
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
		 //设置页面不缓存 ，Cache-Control/Pragma优先级最高
        response.setHeader("Pragma", "no-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0);  
        String verifyCode = VerifyCodeUtil.generateTextCode(NameUtils.TYPE_NUM_UPPER, 6, null);  
        //将验证码放到HttpSession里面  
        request.getSession().setAttribute(NameUtils.VERIFYCODE_NAME, verifyCode);  
        //设置输出的内容的类型为JPEG图像  
        response.setContentType("image/jpeg");  
        BufferedImage bufferedImage = VerifyCodeUtil.generateImageCode(verifyCode, 130, 30, 3, true, Color.WHITE, Color.BLACK, null);  
        //写如IO，返回给浏览器
        ImageIO.write(bufferedImage, "JPEG", response.getOutputStream());  
	}
}
