日期:2014-05-19 浏览次数:20843 次
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"); } }
![]()
推荐阅读更多>
- java.net.MalformedURLException {
- WTK2.5 自带bluetooth例子的有关问题
- java图形有关问题
- 真的不知道为什么,多谢
- csdn 的SEO有关问题
- java发送邮件解决方法
- 大家看看,这样的水平在厦门能不能找到工作呀?解决思路
- 怎么从Spring 获取 Connection
- 快速排序 递归有关问题 求解。
- 但是他报错: org.hibernate.id.IdentifierGenerationException: ids for this
- Thinking In Java 第四版 上有个地方看不懂,不知道是不是陈吴鹏翻译有关问题~
- 关于constructor的有关问题
- ArrayList转换为二维数组有有关问题
- 怎么在所有客户端上进行显示和方法调用
- 怎么能把浏览器地址栏中正确的路径弄错乱码
- 途经探讨下WEB服务器
- 请教cmpareTo和equals有什么区别吗
- 提问解决办法
- 许久木有来了,看看还有木有认识的
- 继续问个简单的查询知识点有关问题,哥哥们