锘??xml version="1.0" encoding="utf-8" standalone="yes"?>日韩国产综合,国内偷自视频区视频综合,av2020不卡http://www.aygfsteel.com/etlee/鈥斺旂紪紼嬭礬婕極zh-cnWed, 18 Jun 2025 15:35:33 GMTWed, 18 Jun 2025 15:35:33 GMT60鍩轟簬struts鐨勯獙璇佺爜瀹炵幇http://www.aygfsteel.com/etlee/archive/2008/05/28/203643.html娑уⅷ娑уⅷWed, 28 May 2008 13:51:00 GMThttp://www.aygfsteel.com/etlee/archive/2008/05/28/203643.htmlhttp://www.aygfsteel.com/etlee/comments/203643.htmlhttp://www.aygfsteel.com/etlee/archive/2008/05/28/203643.html#Feedback0http://www.aygfsteel.com/etlee/comments/commentRss/203643.htmlhttp://www.aygfsteel.com/etlee/services/trackbacks/203643.html 鈥斺旀湰鏂囨憳鑷充簰鑱旂綉鈥斺?br />

浜х敓4涓獙璇佺爜鐨凙CTION錛?/font>

鈥斺斺斺擵alidateCodeAction.java鈥斺斺斺?/font>

package org.struts.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.commons.lang.RandomStringUtils;

public class ValidateCodeAction extends Action {
聽public ActionForward execute(ActionMapping mapping, ActionForm form,
聽聽聽HttpServletRequest request, HttpServletResponse response) {
聽聽try {
聽聽聽int width = 52;
聽聽聽int height = 22;
聽聽聽// 鍙栧緱涓涓?浣嶉殢鏈哄瓧姣嶆暟瀛楀瓧絎︿覆
聽聽聽String code = RandomStringUtils.random(4, true, true);

聽聽聽// 淇濆瓨鍏ession,鐢ㄤ簬涓庣敤鎴風殑杈撳叆榪涜姣旇緝.
聽聽聽// 娉ㄦ剰姣旇緝瀹屼箣鍚庢竻闄ession.
聽聽聽HttpSession session = request.getSession(true);
聽聽聽session.setAttribute("code", code);

聽聽聽response.setContentType("images/jpeg");
聽聽聽response.setHeader("Pragma", "No-cache");
聽聽聽response.setHeader("Cache-Control", "no-cache");
聽聽聽response.setDateHeader("Expires", 0);

聽聽聽ServletOutputStream out = response.getOutputStream();
聽聽聽BufferedImage image = new BufferedImage(width, height,
聽聽聽聽聽BufferedImage.TYPE_INT_RGB);
聽聽聽Graphics g = image.getGraphics();
聽聽聽// 璁懼畾鑳屾櫙鑹?br />聽聽聽g.setColor(getRandColor(200, 250));
聽聽聽g.fillRect(0, 0, width, height);

聽聽聽// 璁懼畾瀛椾綋
聽聽聽Font mFont = new Font("Times New Roman", Font.BOLD, 18);// 璁劇疆瀛椾綋
聽聽聽g.setFont(mFont);

聽聽聽// 鐢昏竟妗?br />聽聽聽// g.setColor(Color.BLACK);
聽聽聽// g.drawRect(0, 0, width - 1, height - 1);

聽聽聽// 闅忔満浜х敓騫叉壈綰匡紝浣垮浘璞′腑鐨勮璇佺爜涓嶆槗琚叾瀹冪▼搴忔帰嫻嬪埌
聽聽聽g.setColor(getRandColor(160, 200));
聽聽聽// 鐢熸垚闅忔満綾?br />聽聽聽Random random = new Random();
聽聽聽for (int i = 0; i < 155; i++) {
聽聽聽聽int x2 = random.nextInt(width);
聽聽聽聽int y2 = random.nextInt(height);
聽聽聽聽int x3 = random.nextInt(12);
聽聽聽聽int y3 = random.nextInt(12);
聽聽聽聽g.drawLine(x2, y2, x2 + x3, y2 + y3);
聽聽聽}

聽聽聽// 灝嗚璇佺爜鏄劇ず鍒板浘璞′腑
聽聽聽g.setColor(new Color(20 + random.nextInt(110), 20 + random
聽聽聽聽聽.nextInt(110), 20 + random.nextInt(110)));

聽聽聽g.drawString(code, 2, 16);

聽聽聽// 鍥捐薄鐢熸晥
聽聽聽g.dispose();
聽聽聽// 杈撳嚭鍥捐薄鍒伴〉闈?br />聽聽聽ImageIO.write((BufferedImage) image, "JPEG", out);
聽聽聽out.close();
聽聽} catch (Exception e) {
聽聽聽e.printStackTrace();
聽聽}
聽聽return null;
聽}

聽private Color getRandColor(int fc, int bc) { // 緇欏畾鑼冨洿鑾峰緱闅忔満棰滆壊
聽聽Random random = new Random();
聽聽if (fc > 255)
聽聽聽fc = 255;
聽聽if (bc > 255)
聽聽聽bc = 255;
聽聽int r = fc + random.nextInt(bc - fc);
聽聽int g = fc + random.nextInt(bc - fc);
聽聽int b = fc + random.nextInt(bc - fc);
聽聽return new Color(r, g, b);
聽}

}

struts-config.xml涓厤緗細

聽<action聽 path="/ValidateCode" type="org.struts.action.ValidateCodeAction" />聽

JSP涓皟鐢ㄧ殑鏂規硶錛?/font>

<html:img src="/ValidateCode.do" border="0"
聽聽聽聽onclick="this.src.value='/ValidateCode.do'"
聽聽聽聽alt="璇瘋緭鍏ユ楠岃瘉鐮侊紝濡傜湅涓嶆竻璇風偣鍑誨埛鏂般? style="cursor: pointer"/>



娑уⅷ 2008-05-28 21:51 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 聂荣县| 敦煌市| 锦屏县| 栖霞市| 定南县| 积石山| 平武县| 天长市| 北辰区| 凤山县| 洪洞县| 毕节市| 澳门| 东丰县| 灵武市| 武平县| 罗田县| 从江县| 青铜峡市| 迭部县| 德钦县| 绥阳县| 阳东县| 开封县| 宜黄县| 大石桥市| 射阳县| 乌拉特前旗| 朝阳县| 观塘区| 大足县| 常山县| 周至县| 寿光市| 平顺县| 桐柏县| 丰台区| 靖江市| 衡南县| 永德县| 连南|