調用生成驗證碼方法時候需要加上時間戳,這樣驗證碼會及時更新,比如$("#xx").attr("src", "xxx.actiont?" + new Date().getTime());
第一種方法:方法寫在一個類中,然後調用這個類來獲取驗證碼
/**
* 驗證碼
* Date 2015-1-7
* Project
* FileName VerifyCode
*/
public class VerifyCode {
// 字符寬度
private int w = 70;
// 字符高度
private int h = 35;
private Random r = new Random();
// 字體類型
private String[] fontNames = {"宋體", "華文楷體", "黑體", "微軟雅黑", "楷體_GB2312"};
private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
private Color bgColor = new Color(255, 255, 255);
private String text ;
// 生成隨機字體顏色
private Color randomColor () {
int red = r.nextInt(150);
int green = r.nextInt(150);
int blue = r.nextInt(150);
return new Color(red, green, blue);
}
// 生成隨機字體大小
private Font randomFont () {
int index = r.nextInt(fontNames.length);
String fontName = fontNames[index];
int style = r.nextInt(4);
int size = r.nextInt(5) + 24;
return new Font(fontName, style, size);
}
// 產生隨機的線條
private void drawLine (BufferedImage image) {
int num = 3;
Graphics2D g2 = (Graphics2D)image.getGraphics();
for(int i = 0; i < num; i++) {
int x1 = r.nextInt(w);
int y1 = r.nextInt(h);
int x2 = r.nextInt(w);
int y2 = r.nextInt(h);
g2.setStroke(new BasicStroke(1.5F));
g2.setColor(Color.BLUE);
g2.drawLine(x1, y1, x2, y2);
}
}
/**
* 將數字改爲字符
* @author
* @since 2015-1-7
* @return char
*/
private char randomChar () {
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
/**
* 生成一張圖片
* @author
* @since 2015-1-7
* @return BufferedImage
*/
private BufferedImage createImage () {
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(this.bgColor);
g2.fillRect(0, 0, w, h);
return image;
}
/**
* 將其生成一張驗證碼圖片
* @author
* @since 2015-1-7
* @return BufferedImage
*/
public BufferedImage getImage () {
BufferedImage image = createImage();
Graphics2D g2 = (Graphics2D)image.getGraphics();
StringBuilder sb = new StringBuilder();
// 向圖片中畫4個字符
for(int i = 0; i < 4; i++) {
String s = randomChar() + "";
sb.append(s);
float x = i * 1.0F * w / 4;
g2.setFont(randomFont());
g2.setColor(randomColor());
g2.drawString(s, x, h-5);
}
this.text = sb.toString();
drawLine(image);
return image;
}
/**
* 得到text值
* @author
* @since 2015-1-7
* @return String
*/
public String getText () {
return text;
}
/**
* 輸出驗證碼
* @author
* @param image 圖片
* @param out 輸出流
* @throws IOException
* @since 2015-1-7
* @return void
*/
public static void output (BufferedImage image, OutputStream out)
throws IOException {
ImageIO.write(image, "JPEG", out);
}
}
調用上述方法:
VerifyCode vc = new VerifyCode();
// 獲取一次性驗證碼圖片
BufferedImage image = vc.getImage();
// 把圖片寫到指定流中
VerifyCode.output(image, response.getOutputStream());
// 把文本保存到session中,爲LoginServlet驗證做準備
request.getSession().setAttribute("vCode", vc.getText());
第二種:直接在jsp中寫
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding="UTF-8"%><%
//驗證碼圖片中可以出現的字符集,可根據需要修改
char mapTable[]={
'a','b','c','d','e','f',
'g','h','i','j','k','l',
'm','n','o','p','q','r',
's','t','u','v','w','x',
'y','z','0','1','2','3',
'4','5','6','7','8','9'};
/**
* 參數width爲生成圖片的寬度,參數height爲生成圖片的高度,參數os爲頁面的輸出流*/
int width=80;
int height=22;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 獲取圖形上下文
Graphics g = image.getGraphics();
// 設定背景色g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
//畫邊框g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
// 取隨機產生的認證碼String strEnsure = "";
// 4代表4位驗證碼,如果要生成更多位的認證碼,則加大數值for(int i=0; i<4; ++i) {
strEnsure+=mapTable[(int)(mapTable.length*Math.random())];
}
// 將認證碼顯示到圖像中,如果要生成更多位的認證碼,增加drawString語句g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String str = strEnsure.substring(0,1);
g.drawString(str,8,17);
str = strEnsure.substring(1,2);
g.drawString(str,20,15);
str = strEnsure.substring(2,3);
g.drawString(str,35,18);
str = strEnsure.substring(3,4);
g.drawString(str,45,15);
// 隨機產生10個干擾點Random rand = new Random();
for (int i=0;i<10;i++) {
int x = rand.nextInt(width);
int y = rand.nextInt(height);
g.drawOval(x,y,1,1);
}
// 釋放圖形上下文g.dispose();
// 輸出圖像到頁面ImageIO.write(image, "JPEG", response.getOutputStream());
session.setAttribute("htmlCode", strEnsure);
out.clear();
out=pageContext.pushBody();
%>