struts2中生成验证码


 

action:

package com.xy.action;

import java.awt.Color;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;


public class CodeGenAction extends ActionSupport implements SessionAware,ServletResponseAware,RequestAware
{

 private static int I_WIDTH = 60;
 private static int I_HEIGHT = 18;
 private static Font VALIDATECODE_FONT = new Font("Times   New   Roman ", Font.PLAIN, 18);
 private Map<String,Object> session;
 private HttpServletResponse response;
 private Map<String, Object> request;
 
 public String execute()
 {
  response.setHeader("Pragma", "No-cache");

  response.setHeader("Cache-Control", "no-cache");

  response.setDateHeader("Expires", 0);

  response.setContentType("image/jpeg");

  // 在内存中创建图象
  BufferedImage image = new BufferedImage(I_WIDTH, I_HEIGHT, BufferedImage.TYPE_INT_RGB);
  // 获取图形上下文
  Graphics g = image.getGraphics();
  // 设定背景色
  g.setColor(Color.white);
  g.fillRect(0, 0, I_WIDTH, I_HEIGHT);
  // 画边框
  g.setColor(Color.blue);
  g.drawRect(0, 0, I_WIDTH - 1, I_HEIGHT - 1);
  Long validate = new Long(10000 + Math.round((Math.random() * 90000)));
  String validateCode = validate.toString();
  session.put("validateCode", validateCode);
  
  // 将认证码显示到图象中
  g.setColor(Color.black);
  g.setFont(VALIDATECODE_FONT);
  // g.drawString(rand,10,15);
  g.drawString(validateCode.toString(), 7, 15);
  // 随机产生88个干扰点,使图象中的认证码不易被其它程序探测到
  Random random = new Random();
  for (int iIndex = 0; iIndex < 128; iIndex++)
  {
   int x = random.nextInt(I_WIDTH);
   int y = random.nextInt(I_HEIGHT);
   g.drawLine(x, y, x, y);
  }
  // 图象生效
  g.dispose();
  
  try
  {
   // 输出图象到页面
   ServletOutputStream sos = response.getOutputStream();
   ImageIO.write(image, "jpeg", sos);
   sos.close();
   return SUCCESS;
  }
  catch (Exception e)
  {
   return "error";
  }
 }
 

 public void setSession(Map<String, Object> session)
 {
  this.session = session;
 }

 public void setServletResponse(HttpServletResponse response)
 {
  this.response = response;  
 }

 public void setRequest(Map<String, Object> request)
 {
  this.request = request;
 }

}


struts.xml,值得注意的时,result type是stream类型

<package name="login" namespace="/" extends="struts-default">
 <action name="toCode" class="com.xy.action.CodeGenAction">
  <result type="stream">
   <param name="contentType">image/jpeg</param>
  </result>
 </action>
</package>


jsp:

<td align="left">
<input id="txtCode" type="text" style="width: 150px" />
<img id="imgCode" src="toCode" />
<a href="javascript:change()"> 看不清,换一张</a>
</td>

 function change()
 {
     var img=document.getElementByIdx_x("imgCode");
 img.src="toCode?time="+new Date().getTime();
 }

智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告