今天看了看验证码的实现,图片类型的验证码,其实很简单的,也可以作为项目储备工具吧。
直接上代码了。
一个工具类,一个servlet,一个演示页面:
工具类:
import java.awt.Color;
import java.util.Random;
public class CodeUtil {
public final static Random random = new Random();
private final static int LENGTH = 6;
private final static char[] CODES = {'2','3','4','5','6','7','8','9','A','B','C','D',
'E','F','G','H','J','K','L','M','N','P','Q',
'R','S','T','U','V','W','X','Y','Z'};
public static String getRandomString(){
StringBuffer sb = new StringBuffer(LENGTH);
for(int i=0;i<LENGTH;i++){
sb.append(CODES[random.nextInt(CODES.length)]);
}
return sb.toString();
}
public static Color getRandomColor(){
return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
}
public static Color getReverseColor(Color color){
return new Color(255-color.getRed(),255-color.getGreen(),255-color.getBlue());
}
}
servlet:
package com.bling.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bling.CodeUtil;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 4762683326456103849L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
Random random = new Random();
String randomString = CodeUtil.getRandomString();
request.getSession().setAttribute("CODE", randomString);
int width=100,height=30;
Color frontColor = CodeUtil.getRandomColor(),
backColor = CodeUtil.getReverseColor(frontColor);
BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,22));
g.setColor(frontColor);
g.fillRect(0,0,width,height);
g.setColor(backColor);
g.drawString(randomString, 10,20);
for(int i=0,n=random.nextInt(100);i<n;i++){
g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
}
ServletOutputStream out = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
out.flush();
}
}
演示页面:
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>随即验证码</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<img id="code" src="<%=request.getContextPath()%>/servlet/login"/><a id="nextcode" href="#">看不清?换一张</a>
</body>
<script type="text/javascript">
window.οnlοad=function(){
var alink = document.getElementById("nextcode");
alink.οnclick=function(url){
var img = document.getElementById("code");
var url = img["src"]+"?id="+new Date().getTime();
img["src"]=url;
return false;
}
}
</script>
</html>
写的比较粗糙,其实大部分代码都可以放在工具类里面的。
大家有需要的可以自己优化一下,这里就是一个思路。