實作主要包含兩個類,一個是產生驗證碼,一個是判斷驗證碼輸入是否正確實現原理比較簡單,將漢字和乾擾線生成圖片並將漢字保存到session,前台獲取每次生成的驗證碼圖片進行展示,驗證的時候就取得文字方塊輸入的值傳到後台與session值進行比較,功能就怎麼簡單。 一、產生漢字驗證碼的類別
package com.veryCode;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics; import java.D. awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet. port javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 產生隨機漢字驗證碼
* @author feizi
* @Time 2015-1-30下午3:16:55
*/
public class DrawVeryCode extends HttpServlet {
public class DrawVeryCode extends HttpServlet { 485 846353L;
public static final int WIDTH = 120;
public static final int HEIGHT = 30;
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOIORe. public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");response.setContentType("text/html;charset=utf-8"); (WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
//取得畫布
Graphics g = bi.getGraphics();
//設定背景顏色
setBackGroundound(g); //繪製幹擾線
drawRandomLine(g);
//產生隨機漢字驗證碼
String randomChar = drawRandomChar((Graphics2D)g);//強制轉換
//將產生的隨機漢字驗證驗證碼存進session中進行儲存
request.getSession().setAttribute("veryCode", randomChar);
//設定回應訊息的類型為圖片類型
response.setContentType("image/jpeg");訊息,防止瀏覽器快取
response.setDateHeader("expries", -1);
response.setHeader("Cache-Control", "no-Cache");
response.setHeader("Pargma", "no-Cache ");
//將驗證碼圖片寫到瀏覽器中
ImageIO.write(bi, "jpg", response.getOutputStream());
}
/**
* 設定背景顏色
* @param g
*/
priv
}
/**
* 設定邊框
* @param g
*/
private void. g){
//設定背景顏色
g.setColor(Color.WHITE);
//填充區域
g.fillRect(0, 0, WIDTH, HEIGHT); } private void setBorder(Graphics g){
//設定邊框顏色
g.setColor(Color.BLUE);
//設定邊框區域
g.drawRect(1, 1, WIDTH - 2, HEIGHT - 2, });
/**
* 畫幹擾線
* @param g
*/
private void drawRandomLine(Graphics g){
//設定幹擾線的顏色
g.setColor(Color.GREEN);
//設定線條數並開始繪製線條int i = 0; i int x1 = new Random().nextInt(WIDTH);
int y1 = new Random().nextInt(HEIGHT);
int x2 = new Random().nextInt. (WIDTH);
int y2 = new Random().nextInt(HEIGHT);
//開始畫線
g.drawLine(x1, y1, x2, y2);
}
}
}
* /
private String drawRandomChar(Graphics2D g){
private String drawRandomChar(Graphics2D g){
StringBuffer sb = new StringBuffer();
//設定顏色
g.setColor(Color.RED); , Font.BOLD, 20));
//準備常用漢字集
String base = "u7684u4e00u4e86u662fu6211u4e0du5728u4ebau4eecu6709u67654ed u5230u5927u91ccu8bf4u5c31u53bbu5b50u5f97u4e5fu548c" +
"u90a3u8981u4e0bu770bu5929u65f6u8fc751f
"u597du8fd8u591au6ca1u4e3au53c8u53efu5bb6u5b66u53eau4ee5u4e3bu4f1au6837" 4u9053u5b83" +
「u540eu7136u8d70u5f88u50cfu89c1u4e24u7528u5979u56fdu52a8u8fdbu6210u56de」+「u4ec0u89u4f5cu559 9u7ecfu53d1" +
"u5de5u5411u4e8bu547du7ed9u957fu6c34u51e0u4e49u4e09u58f0u4e8eu9ad8u624b" + 994u57 u654cu4e4bu」6700u5149u4ea7u60c5u8defu5206u603bu6761u767du8bddu4e1cu5e2d“+
”u6b21u4eb2u5982ucu5e2d“+
”u6b21u4eb2u5982u88abu2b1 cu4f7fu519 9" + " 3 9u671bu65b0u5e26u961f" +
"u5148u529bu5b8cu5374u7ad9u4ee3u5458u673au666f4u4e5160 54au5b69u4e07u5c11u76f4u610fu591cu6bd4u9636u8f deu 8f66u91cd" +
"u4fbfu6597u9a6cu54eau5316u592au6307u53d8u793eu4f3cu58ebu80 05U5E72U77F3“ + 76U5 404U516DU6726077F3“ + 76U5 404U516DU6726077 5E9 4U5173U4FE1U89C9U6B65U53CDU5904U8BBBB0U5C06U5343U627E AU7D27” AU7D27“ + D5U9898U5FAU8D76 U4F41576
」 +
「 U8868U591FU6574U8BA4U54CDU96EEAW6D41U672AU573AU8BE 5U5E76U5E76DF1U523B」 + U53E4U9ED1U5447504 6 33u7167u529eu53f2u6539u5386u 8f6cu753bu9020” +
「u5634u6b64u6cbbu5317u5fc5u670du96e8u7a7fu5185u8bc6u 9a8cu4f20u4e1au83dc" +
"u7 22cu77611517450 u901au51b2u5408u7834" +
"u53cbu5ea6u672fu996du516cu65 c1u623fu6781u5357u67aau8bfbu6c 99u5c81u7ebf" + Y u653fu57ceu52b3u843du94b1u7279u56f4u5f1f" +
"u80d cu6559u70edu5c55u5305u6b4cu7c7bu 6e10u5f36570u5f365707c7 4e5u9645u65e7u795eu5ea7u7ae0u5e2eu5566u53d7u7cfbu4ee4u8d f3u975e “ +
”u4f55u725bu53d6u5165u5cb8065u 797u505c” +
“u606fu53e5u533au8863u822cu62a5u53f6u538bu6162 u53d4u80ccu7ec6 ”; Int nextInt() % 30;
//截取漢字
String ch = base.charAt(new Random().nextInt(base.length())) + "";
//追加到sb中
sb.append(ch);
//順時針旋轉
g.rotate(Degree * Math.PI / 180, x, 20);
//開始畫漢字
g.drawString(ch, x, 20);
//逆時針旋轉
g.rotate(-(Degree * Math.PI / 180), x, 20);
x += 30;
}
return sb.toString();
}
public static void main(String[] args) {
String s = "1234565236985";
System.out.println(s.charAt(5) + "");
System.out.println(new Random().nextInt());
}
}
複製代碼
二、驗證前台頁面上輸入的驗證碼資訊
package com.veryCode; HttpServlet pServletRequest;
導入 javax.servlet.
public void doGet(HttpServletRequest 請求,HttpServletResponse 回應)
拋出 ServletException,IOException{
。 text/html;charset= utf-8");
//產生的正確的驗證碼值
String validateC = (String) request.getSession().getAttribute("veryCode");
//瀏覽器中使用者輸入的驗證碼值
String veryCode = request.getParameter("code");
PrintWriter out = response.getWriter();
if(null == veryCode || "".equals(veryCode)){
out.println("請輸入驗證碼. ..");
}else{
if(validateC.equals(veryCode)){
out.print(1);
}else{
out.println("對不起,驗證碼輸入錯誤...");
}
}
out.flush();
out.close();
}
}