前端調(diào)用
<li>
<lable>驗(yàn)證碼:</lable>
<input type="text" name="yzm">
<img alt="" src="/getCode" onclick="refreshCode(this);">
<script type="text/javascript">
//刷新驗(yàn)證碼
function refreshCode(obj){
obj.src = "/getCode?time=" + new Date().getTime();
}
</script>
</li>顯示效果

實(shí)現(xiàn)驗(yàn)證碼類
package tool;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class ImageUtil {
public static Object[] createImage() {
Object[] obj = new Object[2];
int width = 100;
// 驗(yàn)證碼圖片的高度。
int height = 30;
// 驗(yàn)證碼字符個(gè)數(shù)
int codeCount = 4;
int x = 0;
// 字體高度
int fontHeight;
int codeY;
x = width / (codeCount + 1);
fontHeight = height - 2;
codeY = height - 4;
char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y','3', '4', '5', '6', '7', '8', '9','a','b','c','d','e','f','g','h',
'j','k','m','n','r','s','t','u','v','w','x','y'};
//定義圖像buffer
BufferedImage buffImg = new BufferedImage(
width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
//創(chuàng)建一個(gè)隨機(jī)數(shù)生成器類
Random random = new Random();
//將圖像填充為白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
//創(chuàng)建字體,字體的大小應(yīng)該根據(jù)圖片的高度來定。
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight-5);
//設(shè)置字體。
g.setFont(font);
//畫邊框
g.setColor(Color.WHITE);
g.drawRect(0, 0, width - 2, height - 2);
//隨機(jī)產(chǎn)生干擾線,使圖象中的認(rèn)證碼不易被其它程序探測到。
g.setColor(Color.BLACK);
for(int i = 0; i < 5; i++)
{
int xx = random.nextInt(width);
int yy = random.nextInt(height);
int xxl = random.nextInt(12);
int yyl = random.nextInt(12);
g.drawLine(xx, yy, xx + xxl, yy + yyl);
}
//randomCode用于保存隨機(jī)產(chǎn)生的驗(yàn)證碼,以便用戶登錄后進(jìn)行驗(yàn)證。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
//隨機(jī)產(chǎn)生codeCount數(shù)字的驗(yàn)證碼。
for (int i = 0; i < codeCount; i++) {
//得到隨機(jī)產(chǎn)生的驗(yàn)證碼數(shù)字。
String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
//產(chǎn)生隨機(jī)的顏色分量來構(gòu)造顏色值,這樣輸出的每位數(shù)字的顏色值都將不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
//用隨機(jī)產(chǎn)生的顏色將驗(yàn)證碼繪制到圖像中。
g.setColor(new Color(red, green, blue));
g.drawString(strRand, (i + 1) * x, codeY-2);
//將產(chǎn)生的四個(gè)隨機(jī)數(shù)組合在一起。
randomCode.append(strRand);
}
obj[0] = randomCode.toString();
obj[1] = buffImg;
return obj;
}
}控制器調(diào)用:
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tool.ImageUtil;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
@Controller
public class GetValiadateCode {
//生成驗(yàn)證碼圖片
@ResponseBody
@RequestMapping(value = "/getCode", method = RequestMethod.GET)
public void validateCode(HttpServletRequest request, HttpServletResponse response) throws Exception{
//第一個(gè)參數(shù)是生成的驗(yàn)證碼,第二個(gè)參數(shù)是生成的圖片
Object[] objs = ImageUtil.createImage();
//將驗(yàn)證碼存入Session
request.getSession(true).setAttribute("validateCode",objs[0]);
//將圖片輸出給瀏覽器
BufferedImage image = (BufferedImage) objs[1];
// 禁止圖像緩存。
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
OutputStream os = response.getOutputStream();
ImageIO.write(image, "jpeg", os);
os.flush();
}
}