package com.key.common.base.action;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.springframework.beans.factory.annotation.Autowired;

import com.key.common.utils.web.Struts2Utils;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

@Namespaces({ @Namespace("/") })
public class JcaptchaAction extends ActionSupport {
	
	@Autowired  
    private ImageCaptchaService imageCaptchaService;  
	
	public String execute() throws Exception {
		HttpServletRequest request = Struts2Utils.getRequest();
		HttpServletResponse response = Struts2Utils.getResponse();
        byte[] captchaChallengeAsJpeg = null;  
        // the output stream to render the captcha image as jpeg into  
        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();  
        try {  
            // get the session id that will identify the generated captcha.  
            // the same id must be used to validate the response, the session id  
            // is a good candidate!  
            String captchaId = request.getSession().getId();  
            // call the ImageCaptchaService getChallenge method  
            BufferedImage challenge = imageCaptchaService.getImageChallengeForID(captchaId, request.getLocale());
            // a jpeg encoder  
            JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);  
            jpegEncoder.encode(challenge);
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }
        captchaChallengeAsJpeg = jpegOutputStream.toByteArray();    
        // flush it in the response    
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        ServletOutputStream responseOutputStream = response.getOutputStream();
        responseOutputStream.write(captchaChallengeAsJpeg);
        responseOutputStream.flush();
        responseOutputStream.close();
        return null;
	}  
	
	
	/*@Autowired
	private GenericManageableCaptchaService captchaService;

	@Override
	public String execute() throws Exception {
		HttpServletRequest request = Struts2Utils.getRequest();
		HttpServletResponse response = Struts2Utils.getResponse();
		byte[] captchaChallengeAsJpeg = null;
		// 输出jpg的字节流
		ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
		try {
			String captchaId = request.getSession().getId();
			// call the ImageCaptchaService getChallenge method
			BufferedImage challenge = (BufferedImage) captchaService
					.getChallengeForID(captchaId, request.getLocale());
			JPEGImageEncoder jpegEncoder = JPEGCodec
					.createJPEGEncoder(jpegOutputStream);
			jpegEncoder.encode(challenge);

		} catch (IllegalArgumentException e) {
			response.sendError(response.SC_NOT_FOUND);
			return null;
		} catch (CaptchaServiceException e) {
			response.sendError(response.SC_INTERNAL_SERVER_ERROR);
			return null;
		}
		System.out.println("resImg..");
		captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
		// flush it in the response
		response.setHeader("Cache-Control", "no-store");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		ServletOutputStream responseOutputStream = response.getOutputStream();
		responseOutputStream.write(captchaChallengeAsJpeg);
		responseOutputStream.flush();
		responseOutputStream.close();
		return null;
	}
	
	*//**
	 * 适用ajax异步请求判断验证码对与不对
	 *//*
	public String ajaxCheckCode() throws Exception{
		HttpServletRequest request=Struts2Utils.getRequest();
		HttpServletResponse response=Struts2Utils.getResponse();
		String code = request.getParameter("code");
		System.out.println("code:"+code);
		String result="true";
		try{
			if (!captchaService.validateResponseForID(request.getSession().getId(),code)){
				//验证码不正确
				result="false";
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		PrintWriter out=response.getWriter();
		out.write(result);
		System.out.println("result..."+result);
		return null;
	}
*/
}

@@ -107,24 +107,55 @@
 tStream;%0D%0A%0D%0A
+import javax.imageio.ImageIO;%0D%0A
 import javax
@@ -185,16 +185,16 @@
 tream;%0D%0A
-
 import j
@@ -619,24 +619,26 @@
 onSupport;%0D%0A
+//
 import com.s
@@ -669,16 +669,18 @@
 Codec;%0D%0A
+//
 import c
@@ -1050,24 +1050,67 @@
 esponse();%0D%0A
+        ByteArrayOutputStream out = null;%0D%0A
         byte
@@ -1794,20 +1794,45 @@
  encoder
-  
 %0D%0A
+/*** jdk1.7%E4%B9%8B%E5%90%8E%E9%BB%98%E8%AE%A4%E4%B8%8D%E6%94%AF%E6%8C%81%E4%BA%86 **/%0D%0A//
         
@@ -1912,20 +1912,20 @@
 Stream);
-  
 %0D%0A
+//
         
@@ -1956,24 +1956,117 @@
 hallenge);%0D%0A
+%0D%0A//            %E6%8D%A2%E6%88%90%E6%96%B0%E7%89%88%E5%9B%BE%E7%89%87api%0D%0A            ImageIO.write(challenge, %22jpg%22, jpegOutputStream);%0D%0A%0D%0A
         %7D ca

解决jdk1.7会出现的com.sun.image.codec.jpeg不存在