登录验证码

package com.born.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public final class ImageUtil {
private static final int SIZE = 4;
private static final int LINES = 5;
private static final int WIDTH = 80;
private static final int HEIGHT = 40;
private static final int FONT_SIZE = 30;

/**
* 生成验证图片,封装与Map中。 其中Map的key是验证码,Map的value是验证码图片
*/
public static Map<String, BufferedImage> createImage() {
final char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' };
StringBuffer sb = new StringBuffer();
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics graphic = image.getGraphics();
graphic.setColor(Color.LIGHT_GRAY);
graphic.fillRect(0, 0, WIDTH, HEIGHT);
Random ran = new Random();
// 画随机字符
for (int i = 1; i <= SIZE; i++) {
int r = ran.nextInt(chars.length);
graphic.setColor(getRandomColor());
graphic.setFont(new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE));
graphic.drawString(chars[r] + "", (i - 1) * WIDTH / SIZE, HEIGHT / 2);
sb.append(chars[r]);// 将字符保存,存入Session
}
// 画干扰线
for (int i = 1; i <= LINES; i++) {
graphic.setColor(getRandomColor());
graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH), ran.nextInt(HEIGHT));

}
System.out.println(2345);
Map<String, BufferedImage> map = new HashMap<String, BufferedImage>();
map.put(sb.toString(), image);
return map;
}

/**
* 将图片转化为输入流
*
* @return
* @throws IOException
* @throws ImageFormatException
*/
public static InputStream getInputStream(BufferedImage image) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
byte[] imageBts = bos.toByteArray();
InputStream in = new ByteArrayInputStream(imageBts);
return in;
}

private static Color getRandomColor() {
Random ran = new Random();
Color color = new Color(ran.nextInt(256), ran.nextInt(256), ran.nextInt(256));
return color;
}

}

 

 

 

package com.born.action;

import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.Map;

import com.born.util.ImageUtil;

public class CreateImageAction extends BaseAction {
/**
* 验证码图片输入流
*/
private InputStream imageStream;

public String execute() {
// 创建验证图片
Map<String, BufferedImage> imageMap = ImageUtil.createImage();

// 取出验证码,放入Session
String code = imageMap.keySet().iterator().next();
session.put("imageCode", code);
// 取出图片
BufferedImage image = imageMap.get(code);
try {
// 将图片转化为输入流
imageStream = ImageUtil.getInputStream(image);
} catch (Exception e) {
e.printStackTrace();
return "error";
}
return "success";
}

public InputStream getImageStream() {
return imageStream;
}

public void setImageStream(InputStream imageStream) {
this.imageStream = imageStream;
}

}

 

 

package com.netctoss.action;

import com.netctoss.dao.DAOFactory;
import com.netctoss.dao.ILoginDao;
import com.netctoss.entity.Admin;

/**
 *	登录校验Action
 */
public class LoginAction extends BaseAction {

	// input
	private String adminCode;// 账号
	private String password;// 密码
#cold_bold	private String verifyCode;// 验证码

	// output
	private String errorMsg;// 错误信息

	public String execute() {
#cold_bold		// 从session中取出生成的验证码
#cold_bold		String imageCode = (String) session.get("imageCode");
#cold_bold		// 验证用户输入的验证码是否与生成验证码一致
#cold_bold		if(imageCode == null || !imageCode.equalsIgnoreCase(verifyCode)) {
#cold_bold			//如果不一致,提示错误
#cold_bold			errorMsg = "验证码有误.";
#cold_bold			return "fail";
#cold_bold		}
		
		// 此处略去登录验证逻辑...
	}

	public String getAdminCode() {
		return adminCode;
	}

	public void setAdminCode(String adminCode) {
		this.adminCode = adminCode;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

#cold_bold	public String getVerifyCode() {
#cold_bold		return verifyCode;
#cold_bold	}
#cold_bold
#cold_bold	public void setVerifyCode(String verifyCode) {
#cold_bold		this.verifyCode = verifyCode;
#cold_bold	}

}



<script type="text/javascript" language="javascript">
        	//刷新验证码图片
			function change(image){
				//改变img的src即可,由于该URL并没有变化,因此追加动态参数伪装成变化的URL。
        		image.src = "createImage?date=" + new Date().getTime();
        	}
		</script>




 <tr>
	                    <td class="login_info">验证码:</td>
#cold_bold	                    <td class="width70"><input name="verifyCode" type="text" class="width70" /></td>
	                    <td><img src="createImage" alt="验证码" title="点击更换" onclick="change(this);"/></td>  
	                    <td><span class="required"></span></td>              
	                </tr>            
posted @ 2015-02-09 09:27  萧雪痕  阅读(310)  评论(0编辑  收藏  举报