📄 validateimageaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.xfaccp.struts.action.emp;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* MyEclipse Struts
* Creation date: 11-01-2007
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class ValidateImageAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
private Font mFont=new Font("Times New Roman", Font.PLAIN,18);//设置字体
private int width=60;
private int height=20;
/*这是验证码的另一种方法,不知道怎么用
* 作者:周云*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out.print("++++++++++++++++++++++");
try{
ServletOutputStream out=response.getOutputStream();
BufferedImage bfImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g=bfImage.getGraphics();
//画图片的背景
g.setColor(randColor(200,250));
g.fillRect(0,0,width,height);
//画图片的边框
g.setColor(new Color(0));
g.drawRect(0,0,width-1,height-1);
//画干扰线
Random rand=new Random();
for(int i=0;i<155;i++)
{g.setColor(randColor(160,200));
int x=rand.nextInt(width);
int y=rand.nextInt(height);
int xend=rand.nextInt(12);
int yend=rand.nextInt(12);
g.drawLine(x,y,x+xend,y+yend);
}
//画验证码
g.setFont(mFont);
String num="";
for(int i=0;i<4;i++)
{
String n=String.valueOf(rand.nextInt(10));
g.setColor(new Color(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255)));
g.drawString(n,13*i+6,16);
num+=n;
}
// HttpSession session=request.getSession();
//session.setAttribute("vaildataImage",num);
JPEGImageEncoder jpegCoder=JPEGCodec.createJPEGEncoder(out);
jpegCoder.encode(bfImage);
out.close();
/* 这个Session到底要传什么,才能在登录页面显示验证码?*/
//session.setAttribute("vaildate",bfImage);
return mapping.findForward("login");
}catch(Exception e){
e.printStackTrace();
return mapping.findForward("error");
}
}
public Color randColor(int fc,int bc)
{
fc=fc>255?255:fc;
bc=bc>255?255:bc;
Random rand=new Random();
int r=fc+rand.nextInt(bc-fc);
int g=fc+rand.nextInt(bc-fc);
int b=fc+rand.nextInt(bc-fc);
Color c=new Color(r,g,b);
return c;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -