校验码
来源:互联网 发布:美妆软件哪个好 编辑:程序博客网 时间:2024/05/01 14:36
/** * <校验码> * * @param request * @param response * @throws IOException * @author Administrator * @date 2010-5-12 */ private void verifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("image/jpeg"); response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); HttpSession session = request.getSession(); // 在内存中创建图象 int width = 60, height = 18; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 获取图形上下文 Graphics g = image.getGraphics(); // 设定背景色 g.setColor(new Color(0xffffff)); g.fillRect(0, 0, width, height); String mapTable[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; StringBuffer rands = new StringBuffer(); Random ran = new Random(); for (int i = 0; i < 4; ++i) { rands.append(mapTable[ran.nextInt(mapTable.length)]); } String rand = rands.toString(); // 将认证码存入SESSION session.setAttribute("rand", rand); // 将认证码显示到图象中 g.setColor(Color.black); g.setFont(new Font("Times New Roman", Font.BOLD, 16)); String Str = rand.substring(0, 1); g.drawString(Str, 13 * 0 + 6, 12); Str = rand.substring(1, 2); g.drawString(Str, 13 * 1 + 6, 17); Str = rand.substring(2, 3); g.drawString(Str, 13 * 2 + 6, 15); Str = rand.substring(3, 4); g.drawString(Str, 13 * 3 + 6, 15); // Str = rand.substring(4,5); // g.drawString(Str,65,15); // 随机产生88个干扰点,使图象中的认证码不易被其它程序探测到 Random random = new Random(); for (int i = 0; i < 3; i++) { Color c5 = new Color(random.nextInt(200), random.nextInt(200), random.nextInt(200)); g.setColor(c5); int x = random.nextInt(width / 2); int y = random.nextInt(height / 2); int xl = random.nextInt(60); int yl = random.nextInt(20); g.drawLine(x, y, x + xl, y + yl); } // Random random = new Random(); for (int i = 0; i < 68; i++) { Color c6 = new Color(random.nextInt(250), random.nextInt(250), random.nextInt(200)); g.setColor(c6); int x = random.nextInt(width); int y = random.nextInt(height); g.drawOval(x, y, 0, 0); } // 图象生效 g.dispose(); // 输出图象到页面 ServletOutputStream responseOutputStream = response.getOutputStream(); // 输出图象到页面 ImageIO.write(image, "JPEG", responseOutputStream); // 以下关闭输入流! responseOutputStream.flush(); responseOutputStream.close(); image = null; }
0 0
- 校验码
- 校验码
- 校验码
- 校验码
- 校验码
- 校验码
- 校验码
- 校验码
- 校验码
- 【校验码】数据校验码
- 校验码之奇偶校验码
- 彩色校验码
- 校验码类
- 校验码图片
- 生成校验码
- 校验码知识
- CRC校验码
- CRC校验码
- android TextAppearanceSpan 使用注意事项。
- 黑马程序员---泛型,Collections,Arrays
- 如何中断一个正在运行的线程
- leetcode | Minimum Depth of Binary Tree
- NSOperationQueue和GCD的区别,以及在什么场合下使用
- 校验码
- 有道词典
- 调用有道词典查词
- 安装打印机时出现"无法安装,打印处理器不存在"
- 最大子向量和(连续子数组的最大和_1372
- 手游接入Facebook的那些坑
- 解读HTTP的META标签
- 无法定位序数 xxxx 与动态链接库 avcodec-55.dll上
- 日志文件logback