java web 生成验证码_java web 验证码生成

1 packagecom.act262.demo;2

3 importjava.awt.Color;4 importjava.awt.Font;5 importjava.awt.Graphics;6 importjava.awt.Graphics2D;7 importjava.awt.image.BufferedImage;8 importjava.io.IOException;9 importjava.io.OutputStream;10 importjava.util.Random;11

12 importjavax.imageio.ImageIO;13 importjavax.servlet.ServletException;14 importjavax.servlet.annotation.WebServlet;15 importjavax.servlet.http.HttpServlet;16 importjavax.servlet.http.HttpServletRequest;17 importjavax.servlet.http.HttpServletResponse;18 importjavax.servlet.http.HttpSession;19

20 importsun.java2d.loops.DrawLine;21

22 /**

23 * 输出随机的验证码24 */

25 @WebServlet({ "/CheckCode", "/checkCode.jpg"})26 public class CheckCodeServlet extendsHttpServlet {27 private static final long serialVersionUID = 1L;28

29 /*宽度*/

30 private final int WIDTH = 100;31 /*高度*/

32 private final int HEIGHT = 20;33 /*生成验证码的个数*/

34 private final int COUNT = 4;35 /*干扰线条数*/

36 private final int LINE_ROW = 6;37

38 /*输出的基本码表,如果使用中文,则使用utf-8的码表,类似 \ue234 ,而且应该使用常用字,避免出现偏僻字*/

39 private final char[] BASECODE = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',40 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',41 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',42 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',43 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7',44 '8', '9'};45

46 //写出数据

47 private voidwrite(HttpServletRequest request, HttpServletResponse response)48 throwsIOException {49 HttpSession session =request.getSession();50

51 BufferedImage image = newBufferedImage(WIDTH, HEIGHT,52 BufferedImage.TYPE_INT_RGB);53 Graphics graphics =image.getGraphics();54

55 setBackground(graphics);56 drawBorder(graphics);57 drawDot(graphics);58 drawLine(graphics);59 drawString(graphics, session);60

61 //写出数据流

62 ImageIO.write(image, "jpg", response.getOutputStream());63

64 }65

66 //写字

67 private voiddrawString(Graphics graphics, HttpSession session) {68 StringBuffer sb = newStringBuffer();69 Random random = newRandom();70

71 graphics.setFont(new Font("宋体", Font.BOLD, 18));72 graphics.setColor(Color.BLACK);73

74 for (int i = 0; i < COUNT; i++) {75 String ch =String76 .valueOf(BASECODE[random.nextInt(BASECODE.length)]);77 sb.append(ch);78

79 //设置位置

80 int x = i * 20 + random.nextInt(12) + 10;81 int y = random.nextInt(HEIGHT / 3) + 12;82

83 //旋转字体

84 double theta = Math.PI / 180 * random.nextInt(20);85 //rotate(graphics, theta);

86

87 graphics.drawString(ch, x, y);88

89 //恢复。。90 //rotate(graphics, -theta);

91 }92 session.setAttribute("checkCode", sb.toString());93

94 System.out.println("session:" + session + " 验证码:" +sb.toString());95 }96

97 //旋转

98 private void rotate(Graphics graphics, doubletheta) {99 ((Graphics2D) graphics).rotate(theta);100 }101

102 //画随机线条

103 private voiddrawLine(Graphics graphics) {104 Random random = newRandom();105 for (int i = 0; i < LINE_ROW; i++) {106 int x1 =random.nextInt(WIDTH);107 int y1 =random.nextInt(HEIGHT);108 int x2 =random.nextInt(WIDTH);109 int y2 =random.nextInt(HEIGHT);110 setRandomColor(graphics);111 graphics.drawLine(x1, y1, x2, y2);112 }113 }114

115 //画斑点

116 private voiddrawDot(Graphics graphics) {117 Random random = newRandom();118 graphics.setColor(Color.red);119 for (int i = 0; i < WIDTH; i++) {120 int x =i;121 int y =random.nextInt(HEIGHT);122 int r = random.nextInt(2);123 //graphics.fillOval(x, y, r, r);

124 graphics.drawOval(x, y, r, r);125 }126 }127

128 //画边框

129 private voiddrawBorder(Graphics graphics) {130 graphics.setColor(Color.BLACK);131 graphics.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);132 }133

134 //设置背景

135 private voidsetBackground(Graphics graphics) {136 graphics.setColor(Color.WHITE);137 graphics.fillRect(0, 0, WIDTH, HEIGHT);//填充背景色

138 }139

140 //设置随机的画笔颜色

141 private voidsetRandomColor(Graphics g) {142 Random random = newRandom();143 g.setColor(new Color(random.nextInt(255), random.nextInt(255), random144 .nextInt(255)));145 }146

147 protected voiddoGet(HttpServletRequest request,148 HttpServletResponse response) throwsServletException, IOException {149

150 //输出图片流的头信息

151 response.setContentType("image/jpeg");152 response.setHeader("Expires", "-1");153 response.setHeader("Cache-Control", "no-cache");154

155 //写出数据

156 write(request, response);157

158 }159

160 protected voiddoPost(HttpServletRequest request,161 HttpServletResponse response) throwsServletException, IOException {162 //TODO Auto-generated method stub

163 }164

165 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值