1、3种使用java生成验证码的方法java /image.jsp 255) fc = 255; if (bc 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); % /设置页面不缓存 response.setHeader(Pragma, No-cache); response.setHeader(Cache-Control, no-cach
2、e); response.setDateHeader(Expires, 0); / 在内存中创建图象 / 通过这里可以修改图片大小 int width = 85, height = 23; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); / 获取图形上下文 / g相当于笔 Graphics g = image.getGraphics(); /生成随机类 Random random = new Random(); / 设定背景色 g.setColor(getRandColor(
3、200, 250); / 画一个实心的长方,作为北京 g.fillRect(0, 0, width, height); /设定字体 g.setFont(new Font(黑体, Font.PLAIN, 18); /画边框 g.setColor(Color.BLUE); g.drawRect(0,0,width-1,height-1); / 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 g.setColor(getRandColor(160, 200); for (int i = 0; i 155; i+) int x = random.nextInt(width); int y
4、 = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); / 取随机产生的认证码(4位数字) /String rand = request.getParameter(rand); /rand = rand.substring(0,rand.indexOf(.); String sRand = ; / 如果要使用中文,必须定义字库,可以使用数组进行定义 / 这里直接写中文会出乱码,必须将中文转换为unicode编码 S
5、tring str = A, B, C, D, E, F, G, H, J, K, L, M, N, P, Q, R, S, T, U, V, W, X, Y, Z, a, b, c, d, e, f, g, h, i, j, k, m, n, p, s, t, u, v, w, x, y, z, 1, 2, 3, 4, 5, 6, 7, 8, 9 ; for (int i = 0; i /使用验证码的页面login.jsp 登陆页面 function reloadImage() document.getElementById(identity).src = image.jsp?ts= + n
6、ew Date().getTime(); 登陆程序 用户ID: 密 码: 验证码: /image.jsp 255) fc = 255; if (bc 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); % /设置页面不缓存 response.setHeader(Pragma, No-cache); response.
7、setHeader(Cache-Control, no-cache); response.setDateHeader(Expires, 0); / 在内存中创建图象 / 通过这里可以修改图片大小 int width = 85, height = 23; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); / 获取图形上下文 / g相当于笔 Graphics g = image.getGraphics(); /生成随机类 Random random = new Random();
8、/ 设定背景色 g.setColor(getRandColor(200, 250); / 画一个实心的长方,作为北京 g.fillRect(0, 0, width, height); /设定字体 g.setFont(new Font(黑体, Font.PLAIN, 18); /画边框 g.setColor(Color.BLUE); g.drawRect(0,0,width-1,height-1); / 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 g.setColor(getRandColor(160, 200); for (int i = 0; i 155; i+) int
9、x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); / 取随机产生的认证码(4位数字) /String rand = request.getParameter(rand); /rand = rand.substring(0,rand.indexOf(.); String sRand = ; / 如果要使用中文,必须定义字库,可以使用数组进行定义 /
10、 这里直接写中文会出乱码,必须将中文转换为unicode编码 String str = A, B, C, D, E, F, G, H, J, K, L, M, N, P, Q, R, S, T, U, V, W, X, Y, Z, a, b, c, d, e, f, g, h, i, j, k, m, n, p, s, t, u, v, w, x, y, z, 1, 2, 3, 4, 5, 6, 7, 8, 9 ; for (int i = 0; i /使用验证码的页面login.jsp 登陆页面 function reloadImage() document.getElementById(i
11、dentity).src = image.jsp?ts= + new Date() .getTime(); 登陆程序 用户ID: 密码: 验证码: 效果如下:(2)、使用Servlet生成验证码java /IdentityServlet.java代码如下:package com.helloweenvsfei.servlet;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;impo
12、rt java.util.Random;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;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.jpe
13、g.JPEGImageEncoder;public class IdentityServlet extends HttpServlet /* * */ private static final long serialVersionUID = -479885884254942306L; public static final char CHARS = 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, G, H, J, K, L, M, N, P, Q, R, S, T, U, V, W, X, Y, Z ; public static Random random
14、 = new Random(); public static String getRandomString() StringBuffer buffer = new StringBuffer(); for (int i = 0; i 6; i+) buffer.append(CHARSrandom.nextInt(CHARS.length); return buffer.toString(); public static Color getRandomColor() return new Color(random.nextInt(255), random.nextInt(255), random
15、 .nextInt(255); public static Color getReverseColor(Color c) return new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c .getBlue(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.setContentType(image/jpeg); String randomS
16、tring = getRandomString(); request.getSession(true).setAttribute(randomString, randomString); int width = 100; int height = 30; Color color = getRandomColor(); Color reverse = getReverseColor(color); BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.c
17、reateGraphics(); g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16); g.setColor(color); g.fillRect(0, 0, width, height); g.setColor(reverse); g.drawString(randomString, 18, 20); for (int i = 0, n = random.nextInt(100); i n; i+) g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1); / 转成JPE
18、G格式 ServletOutputStream out = response.getOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(bi); out.flush(); public static void main(String args) System.out.println(getRandomString(); /Web.xml的配置为: IdentityServlet com.helloweenvsfei.servlet.IdentityServlet IdentityServlet /servlet/IdentityServlet/测试页面identity.html为: identity.html !- funct
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1