struts中验证码的实现

public class ValidateCodeAction {
	private ByteArrayInputStream byteArrayInputStream;

	public ByteArrayInputStream getByteArrayInputStream() {
		return byteArrayInputStream;
	}

	public void setByteArrayInputStream(
			ByteArrayInputStream byteArrayInputStream) {
		this.byteArrayInputStream = byteArrayInputStream;
	}

	public String execute() {
		try {
			setByteArrayInputStream(createValidateCodeImage());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	/**
	 * 产生一个验证码图片
	 * 
	 * @return ByteArrayInputStream
	 */
	private ByteArrayInputStream createValidateCodeImage() {
		BufferedImage bufferedImage = new BufferedImage(100, 30,
				BufferedImage.TYPE_INT_RGB);
		Graphics g = bufferedImage.getGraphics();
		g.setColor(Color.white);
		g.fillRect(0, 0, 100, 30);
		drawBackground(g);// 绘制图片背景
		drawValidateCode(g);// 绘制生成的验证码到图片上
		ByteArrayInputStream byteArrayInputStream = null;
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		try {
			ImageIO.write(bufferedImage, "JPEG", byteArrayOutputStream);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("产生一个验证码图片");
		}
		byte[] buf = byteArrayOutputStream.toByteArray();
		byteArrayInputStream = new ByteArrayInputStream(buf);
		try {
			byteArrayOutputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return byteArrayInputStream;
	}

	/**
	 * 绘制图片背景
	 * 
	 * @param g
	 * */
	private void drawBackground(Graphics g) {
		Random random = new Random();
		int randomX, randomY;
		for (int i = 0; i < random.nextInt(50)+500; ++i) {
			g.setColor(new Color(random.nextInt(255), random.nextInt(255),
					random.nextInt(255)));
			randomX = random.nextInt(100);
			randomY = random.nextInt(30);
			g.drawLine(randomX, randomY, randomX, randomY);
		}
	}

	/**
	 * 产生随机验证码,并存入Session发送至服务器端
	 * 
	 * @return String
	 * */
	private String createCode() {
		Random random = new Random();
		StringBuffer sb = new StringBuffer(
				"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");// 验证码范围
		StringBuffer code = new StringBuffer();
		int codeLength = 4;// 验证码长度
		for (int i = 0; i < codeLength; ++i) {
			code.append(sb.charAt(random.nextInt(sb.length())));
		}
		// 将验证码存入Session
		ServletActionContext.getRequest().getSession().setAttribute(
				Constant.VALIDATE_CODE, code.toString());
		return code.toString();
	}

	/**
	 * 将验证码绘制到图片上
	 * 
	 * @param g
	 * */
	private void drawValidateCode(Graphics g) {
		String code = createCode();
		Random random = new Random();
		int x = 10, y = 0;
		g.setFont(new Font("Times New Roman", Font.PLAIN, 30));
		for (int i = 0; i < code.length(); ++i) {
			y =30-random.nextInt(10);
			g.setColor(new Color(random.nextInt(150), random.nextInt(150),
					random.nextInt(150)));
			g.drawString(code.substring(i, i + 1), x, y);
			x += 20;
		}
	}
}


注意在配置文件中这样配置:

<!-- 验证码解析XML -->
		<action name="validateCodeAction" class="com.weiyi.exam.web.action.ValidateCodeAction">
			<result name="success" type="stream">
				<param name="contentType">image/jpeg</param>
				<param name="inputName">byteArrayInputStream</param>
			</result>
		</action>


每次需要验证码的时候,只要请求此Action

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值