锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
鐜板湪緗戠粶涓婂悇涓綉绔欎笂閮芥湁楠岃瘉鐮?浣嗘槸瀹炵幇鐨勫艦褰㈣壊鑹插悇涓嶇浉鍚?閫氳繃鏌ヨ祫鏂欏拰鎽哥儲,浣跨敤servlet鏉ュ疄鐜伴獙璇佺爜.
涓 寤轟竴涓獙璇佺爜鐢熸垚綾籖andomImageGenerator ,鍏跺姛鑳藉氨鏄疄鐜伴獙璇佺爜
璇ョ被瀹炵幇濡備笅
import java.awt.*;
import java.awt.image.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.apache.commons.lang.RandomStringUtils;
/**
* 闅忓嵆鍥劇墖鐢熸垚鍣?br> * 璇ョ被鐢ㄤ簬鐢ㄦ埛娉ㄥ唽鏃跺欓渶瑕佺敤鎴鋒牴鎹浘鐗囧唴瀹硅繘琛屽~鍐欐紜悗鏂瑰彲娉ㄥ唽
* @author Liudong
*/
public class RandomImageGenerator {
//闅忓嵆鐢熸垚鍖呭惈楠岃瘉鐮佺殑瀛楃涓?nbsp;
public static String random() {
//20060320 add by wyx
//鍥犱負o鍜?,l鍜?寰堥毦鍖哄垎,鎵浠?鍘繪帀澶у皬鍐欑殑o鍜宭
String str = "";
str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";//鍒濆鍖栫瀛?br> return RandomStringUtils.random(6, str);//榪斿洖6涓虹殑瀛楃涓?br> }
/**
* 鏍規(guī)嵁瑕佹眰鐨勬暟瀛楃敓鎴愬浘鐗?鑳屾櫙涓虹櫧鑹?瀛椾綋澶у皬16,瀛椾綋棰滆壊榛戣壊綺椾綋
* @param num 瑕佺敓鎴愮殑鏁板瓧
* @param out 杈撳嚭嫻?br> * @throws IOException
*/
public static void render(String num, OutputStream out) throws IOException {
if (num.getBytes().length > 6) {
throw new IllegalArgumentException(
"The length of param num cannot exceed 6.");
}
//璁懼畾瀹藉害鍜岄珮搴?br> int width = 130;
int height = 30;
// 鍦ㄥ唴瀛樹腑鍒涘緩鍥捐薄
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 鑾峰彇鍥懼艦涓婁笅鏂?br> Graphics2D g = (Graphics2D) bi.getGraphics();
//鐢昏竟妗?br> java.util.Random random = new java.util.Random();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
//璁劇疆瀛椾綋
Font mFont = new Font("Tahoma", Font.BOLD, 16);
g.setFont(mFont);
g.setColor(Color.BLACK);//璁劇疆瀛椾綋棰滆壊
//鐢昏璇佺爜,姣忎釜璁よ瘉鐮佸湪涓嶅悓鐨勬按騫充綅緗?br> String str1[] = new String[6];
for (int i = 0; i < str1.length; i++) {
str1[i] = num.substring(i, i + 1);
int w = 0;
int x = (i + 1) % 3;
//闅忓嵆鐢熸垚楠岃瘉鐮佸瓧絎︾殑姘村鉤鍋忕Щ閲?br> if (x == random.nextInt(3)) {
w = 19 - random.nextInt(7);
} else {
w = 19 + random.nextInt(7);
}
//闅忓嵆鐢熸垚棰滆壊
Color color1 = new Color(random.nextInt(180), random.nextInt(180),
random.nextInt(180));
g.setColor(color1);
g.drawString(str1[i], 20 * i + 10, w);
}
// 闅忔満浜х敓騫叉壈鐐?騫剁敤涓嶅悓鐨勯鑹茶〃紺猴紝浣垮浘璞′腑鐨勮璇佺爜涓嶆槗琚叾瀹冪▼搴忔帰嫻嬪埌
for (int i = 0; i < 100; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
Color color1 = new Color(random.nextInt(255), random.nextInt(255),
random.nextInt(255));
g.setColor(color1); //闅忓嵆鐢誨悇縐嶉鑹茬殑鐐?br> g.drawOval(x, y, 0, 0);
}
//鐢誨共鎵扮嚎
for (int i = 0; i < 5; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
Color color1 = new Color(random.nextInt(255), random.nextInt(255),
random.nextInt(255));
g.setColor(color1); //闅忓嵆鐢誨悇縐嶉鑹茬殑綰?br> g.drawLine(x, y, x1, y1);
}
//鍥懼儚鐢熸晥
g.dispose();
//杈撳嚭欏甸潰
ImageIO.write(bi, "jpg", out);
}
public static void main(String[] args) throws IOException {
String num = random();
System.out.println(num);
render(num, new FileOutputStream("D:\\test.jpg"));
System.out.println("Image generated.");
}
}
浜?楠岃瘉鐮佺殑瀹炵幇,浣跨敤servlet鏉ュ疄鐜伴獙璇佺爜
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import peopleBlog.util.RandomImageGenerator;
/**
* 鐢ㄤ簬浜х敓闅忓嵆鍥劇墖浠ラ槻姝㈤潪娉曟敾鍑?br> * @author Liudong
*/
public class RandomImageServlet extends HttpServlet {
public final static String RANDOM_LOGIN_KEY = "RANDOM_LOGIN_KEY";
public void init() throws ServletException {
System.setProperty("java.awt.headless","true");
}
public static String getRandomLoginKey(HttpServletRequest req) {
return (String)req.getSession().getAttribute(RANDOM_LOGIN_KEY);
}
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
HttpSession ssn = req.getSession();
if(ssn!=null) {
String randomString = RandomImageGenerator.random();//鐢熸垚縐嶅瓙
ssn.setAttribute(RANDOM_LOGIN_KEY,randomString);//灝嗙瀛愭斁鍒皊ession閲岄潰
res.setContentType("image/jpeg");//璁劇疆鍥懼儚鐢熸垚鏍煎紡
RandomImageGenerator.render(randomString,res.getOutputStream());//杈撳嚭鍒伴〉闈?br> }
}
}
鍏朵腑 ssn.setAttribute(RANDOM_LOGIN_KEY,randomString);榪欎竴琛屼唬鐮佺殑浣滅敤鏄?
褰撲粠欏甸潰涓婅緭鍏ラ獙璇佺爜,鎻愪氦鍚?鍦ㄥ悗鍙皊evlet鎴栬卆ction閲岄潰楠岃瘉杈撳叆鐨勯獙璇佺爜鍜宻ession閲岄潰鐨勬槸鍚︿竴鑷?濡傛灉涓嶄竴鑷磋繑鍥為敊璇俊鎭?
涓?欏甸潰瀹炵幇
鍦╳eb.xml鏂囦歡涓緗?/p>
<servlet>
<servlet-name>image</servlet-name>
<servlet-class>peopleBlog.RandomImageServlet</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>image</servlet-name>
<url-pattern>/verifyCode.jsp</url-pattern>
</servlet-mapping>
鐒跺悗鍦╦sp欏甸潰涓?/p>
<td><input name="verifyCode" type="text" id="verifyCode" class="input2" style="width:55px;" onkeydown="JavaScript:ifEnter()"/> <img src="/blog/verifyCode.jsp" align="absMiddle" border="0"/> </td>
鍐嶅湪鐩殑servlet涓繘琛岄獙璇佺爜鍒ゆ柇灝眔k浜?/p>