日期:2014-05-19 浏览次数:20790 次
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"); } }
推荐阅读更多>
- submit以后无法执行servlet,求教,
- ,关于import包的有关问题
- unix下,java 能否实现与客户端的域socket通讯?能的话,怎么实现?请们赐教
- 一个随机移动的小球如何让它从上到下移动
- 帮忙!看下错在哪?该怎么处理
- js、checkbox
- 菜鸟java疑难
- 打开页面后刷新图片不成功!很纠结啊该怎么解决
- 怎样取得表格中的某一行,
- 一些jdbc和DB2编程的有关问题
- java程序运行,初学者有关问题
- 麻烦的数据库设计有关问题,请高手帮忙
- Tomcat有关问题:Document base does not exist or is not a readable directory
- Table的tbody里用innerHTML如何添值啊(已经疯了)
- 大牛们请进咋功能咋实现,该如何解决
- IIS和Resin集成,只能运行jsp,servlet则报找不到页面?解决方案
- 决议从今天起边打基础边学J2EE
- 分割大文本文件的程序,不能正常输出,请各位大神指教,该怎么处理
- 如果把"xxxx<abc>xxx"里的"<abc>"轮换掉?尖括号里的"abc"是不可预知的任意字符
- 郑*来接分,该如何解决