首页>代码>java生成图片验证码>/1329255033308160.java
package com.neil.Hose.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ValidateCode extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public ValidateCode() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * 产生随机数
	 * 
	 * @param number
	 * @return
	 */
	public String randomString(int number) {
		// 生产字符的内容
		String str = "23456789qwertyuipasdfghjklzxcvbnm";
		char[] chars = str.toCharArray();
		int length = chars.length;
		StringBuilder sb = new StringBuilder();
		Random random = new Random();
		// 随机生成number个字符
		for (int i = 0; i < number; i++) {
			// 吧单个字符转换成追加到已经产生的字符串中
			sb.append(new String(new Character(chars[random.nextInt(length)])
					.toString()));
		}

		return sb.toString();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 设置响应格式
		response.setContentType("image/jpeg");
		// 定义一个BuufferedImage对象
		BufferedImage image = new BufferedImage(80, 20,
				BufferedImage.SCALE_SMOOTH);
		// 根据上述对象产生一个Graphics2D上下文
		Graphics2D graphics = image.createGraphics();
		// 将上下文颜色设置为白色
		graphics.setColor(Color.white);
		graphics.fillRect(0, 0, 100, 20);
		// 设置字体
		graphics.setFont(new Font("Roman", Font.CENTER_BASELINE, 20));
		// 产生4个字符的字符串
		String randomStr = randomString(4);
		// 生成的验证码存放到Session中
		request.getSession().setAttribute("showcode", randomStr);
		for (int i = 0; i < randomStr.length(); i++) {
			// 随机设置上下文颜色
			graphics.setColor(new Color(new Random().nextInt(250), new Random()
					.nextInt(250), new Random().nextInt(250)));
			// 按字符把其画出来
			graphics.drawString(randomStr.substring(i, i + 1), 15 * i, 16);
			// 产生干扰线
			graphics.drawLine(7, 7, 7, 7);

		}
		// 禁止缓存代码,没有这段话则不能进行更新显示
		response.setHeader("Prama", "no-cache");
		response.setHeader("Coche-Chontrol", "no-cache");
		response.setDateHeader("Expires", 0);
		// 写入客户端
		ImageIO.write(image, "jpeg", response.getOutputStream());

	}

	public void init() throws ServletException {

	}

}
最近下载更多
hashirush  LV2 2020年5月27日
zhang1328175960  LV2 2019年6月28日
puhuiling  LV9 2017年12月19日
小 丿飞  LV4 2017年9月21日
289355065  LV3 2017年8月25日
1205450362  LV2 2017年8月23日
xijilsq  LV9 2017年7月17日
aihui523  LV34 2017年6月24日
18500331027  LV2 2017年6月22日
561823592  LV1 2017年6月14日
最近浏览更多
2251038975 1月8日
暂无贡献等级
EFWAGGFAWGR 2023年10月18日
暂无贡献等级
3174233007 2022年12月19日
暂无贡献等级
wwww11112222 2022年7月19日
暂无贡献等级
pingfan_yu  LV2 2022年5月17日
听着雨吹着风  LV9 2022年5月8日
liys1234  LV9 2022年4月22日
liuyanlin_sc 2022年4月11日
暂无贡献等级
微信网友_5834010155782144  LV1 2022年2月15日
423256321  LV4 2021年12月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友