日期:2014-05-19 浏览次数:21219 次
package com.chk.cms.checkcode; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.xml.internal.ws.api.ha.StickyFeature; public class CheckCodeServlet extends HttpServlet { // 定义验证码 图片的宽度 private int width; // 定义验证码图片的高度。 private int height; // 显示多少位字符 private int number; // 有哪些字符备选 private String codes; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jepg"); // 创建一张图片 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); // 设置背景色 g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); // 设置黑色边框 g.setColor(Color.BLACK); g.drawRect(0, 0, width - 1, height - 1); // 每个字符占据的宽度和高度 int x = (width - 1) / number; int y = height - 4; // 定义一个StringBuffer,用于存储code字符串,将来放置到session中 StringBuffer sb = new StringBuffer(); // 随机生成字符 Random random = new Random(); for (int i = 0; i < number; i++) { String code = String.valueOf(codes.charAt(random.nextInt(codes.length()))); // 随机生成颜色 int red = random.nextInt(255); int green = random.nextInt(255); int blue = random.nextInt(255); g.setColor(new Color(red, green, blue)); // 设置字体 g.setFont(new Font("宋体", Font.BOLD, 14)); g.drawString(code, i * x + 2, y); sb.append(code); } // 将字符串放置到Http Session中 request.getSession().setAttribute("codes", sb.toString()); // 随机生成一些干扰点 for (int i = 0; i < 50; i++) { int red = random.nextInt(255); int green = random.nextInt(255); int blue = random.nextInt(255); g.setColor(new Color(red, green, blue)); g.drawOval(random.nextInt(width), random.nextInt(height), 1, 1); } OutputStream out = response.getOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void init() throws ServletException { width = Integer.parseInt(this.getInitParameter("width")); height = Integer.parseInt(this.getInitParameter("height")); number = Integer.parseInt(this.getInitParameter("number")); codes = this.getInitParameter("codes"); } }
推荐阅读更多>
-
JAX-RS Web有关问题,浏览器输入URL,发生404异常
-
hibernate 配置文件 pojo 工具 hibernate tools-3.0,该如何处理
-
这几天冷大家伙儿注意别感冒啊
-
一个很诡异的有关问题,queryForInt若动态传参就报错,直接拼到sql中就没有关问题
-
高分关于java验证框架的有关问题
-
疑问:hibernate输出SQL到log4j的有关问题
-
关于java网络编程,该怎么处理
-
在swing中装入word的有关问题
-
List中的toArray的一些有关问题
-
jsp+javabean解决方法
-
jsp在运作的时候报错了
-
求jsp博客源代码mvc+mysql,该如何解决
-
ubuntu环境下tomcat启动正常,程序运行诡异的异常!高手解答!
-
高分 急求!该如何处理
-
父类的静态变量被继承后,子类修改静态变量,为什么父类的也跟着变了?解决思路
-
jsp关于隐藏单元格的有关问题
-
for exception javax.servlet.ServletException: BeanUtils.populate
-
jsp怎么将一个String类型参数传递到弹出窗口
-
请问,小弟我的actionform如何拿不到属性字段里的值。拿的全是空值null
-
mina 解码器中数组溢出有关问题