服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java实现随机验证码具体代码

Java实现随机验证码具体代码

2020-07-21 12:21雪剑残花雨 Java教程

这篇文章主要为大家详细介绍了Java实现随机验证码具体代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java随机生成验证码的具体代码,供大家参考,具体内容如下

Java实现随机验证码具体代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import java.awt.Color;
 
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
 
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
 
public class RandImage extends HttpServlet {
 
/**
* Constructor of the object.
*/
  public RandImage() {
    super();
  }
 
    private int imgWidth = 0; //图片宽度
 
    private int imgHeight = 0; //图片的高度
 
    private int codeCount = 0; //图片里面字符的个数
 
    private int x = 0;
 
    private int fontHeight; //字体的高度
  
    private int codeY;
 
    private String fontStyle; //字体样式
 
 
    //序列化ID 避免重复
    private static final long serialVersionUID = 128554012633034503L;
 
    /**
    * 初始化配置参数
    */
 
public void init() throws ServletException {
 
  // 宽度
  String strWidth = "200";
  // 高度
  String strHeight ="80";
  // 字符个数
  String strCodeCount ="5";
  //字体
  fontStyle = "Times New Roman";
 
// 将配置的信息转换成数值
  try {
    if (strWidth != null && strWidth.length() != 0) {
    imgWidth = Integer.parseInt(strWidth);
    }
  if (strHeight != null && strHeight.length() != 0) {
  imgHeight = Integer.parseInt(strHeight);
    }
  if (strCodeCount != null && strCodeCount.length() != 0) {
  codeCount = Integer.parseInt(strCodeCount);
    }
  } catch (NumberFormatException e) {
    e.printStackTrace();
  }
  
    x = imgWidth / (codeCount + 1); //字符间距
    fontHeight = imgHeight - 2; //字体的高度
    codeY = imgHeight - 12; // 代码高度
  }
 
    protected void processRequest(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
 
    //输出流设置
    response.setContentType("image/jpeg"); //输出格式
    response.setHeader("Pragma", "No-cache");//不缓存 重新生成
    response.setHeader("Cache-Control", "no-cache");//不缓存 重新生成
    response.setDateHeader("Expires", 0); //0秒失效 也是不缓存
    HttpSession session = request.getSession(); //获取session 会话
 
    // 在内存中创建图象
    BufferedImage image = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
    // 获取图形上下文  
    Graphics2D g = image.createGraphics();
    // 生成随机类
    Random random = new Random(); //随机类
    // 设定矩形的背景色
    g.setColor(Color.WHITE);
    //填充矩形Rect为白色
    g.fillRect(0, 0, imgWidth, imgHeight);
 
    // 设定边框字体
    g.setFont(new Font(fontStyle, Font.PLAIN + Font.ITALIC, fontHeight));
    //设置边框的颜色
    g.setColor(new Color(55, 55, 12));
    // 画边框
    g.drawRect(0, 0, imgWidth - 1, imgHeight - 1);
 
    // 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到
    g.setColor(getRandColor(160, 200));
    for (int i = 0; i < 160; i++) {
    int x = random.nextInt(imgWidth);
    int y = random.nextInt(imgHeight);
    int xl = random.nextInt(12);
    int yl = random.nextInt(12);
    g.drawLine(x, y, x + xl, y + yl);
  }
  
    // 取随机产生的认证码(4位数字)
    String sRand = "";
    int red = 0, green = 0, blue = 0;
    for (int i = 0; i < codeCount; i++) { //循环生成codeCount个随机字符
 
    //通过rgb三色随机得到新的颜色
    red = random.nextInt(255);
    green = random.nextInt(255);
    blue = random.nextInt(255);
    //随机得到一个0 1 2 的数字
    int wordType = random.nextInt(3);//随机得到0-2之间的3个数字
    char retWord = 0;
    //0 数字 1 小写字母 2 大写字母
    switch (wordType) {
  case 0:
    retWord = this.getSingleNumberChar(); //得到0-9的char型数字
  break;
  case 1:
    retWord = this.getLowerOrUpperChar(0); //得到小写的char型字母
  break;
  case 2:
    retWord = this.getLowerOrUpperChar(1); //得到大写的char型字母
  break;
  }
    sRand += String.valueOf(retWord); //将得到的随机字符 连接起来
    g.setColor(new Color(red, green, blue)); //设置一个颜色
    g.drawString(String.valueOf(retWord), 2+(i) * x, codeY); //将字符写到图片中 对应的位置
  }
    // 将认证码存入SESSION
    session.setAttribute("rand", sRand); //将得到的随机字符存入到session回话中,验证的时候可以调用
    // 图象生效
    g.dispose(); //释放g 对象
    ServletOutputStream responseOutputStream = response.getOutputStream(); //输出流
    // 输出图象到页面
    ImageIO.write(image, "JPEG", responseOutputStream); //以JPEG的格式输出
 
    // 以下关闭输入流!
    responseOutputStream.flush();//刷新并关闭流
    responseOutputStream.close();
  }
 
    Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色
    Random random = new Random();
    if (fc > 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);
  }
 
    protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
  }
 
    protected void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
  }
 
 
    // 将整型随机数字转换成char返回
    private char getSingleNumberChar() {
    Random random = new Random();
    int numberResult = random.nextInt(10);
    int ret = numberResult + 48; //将字符 '0‘ 转换成ascall码的时候 就是48
    return (char) ret;
  }
 
    //得到26个字符
    private char getLowerOrUpperChar(int upper) {
    Random random = new Random();
    int numberResult = random.nextInt(26);
    int ret = 0;
    if (upper == 0) {// 小写
    ret = numberResult + 97;
  } else if (upper == 1) {// 大写
    ret = numberResult + 65;
    }
    return (char) ret;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐