本文最后更新于:4 个月前
做课设时候,选择是做“图书借阅管理系统”。
在写登陆界面时候,想把前段时间学的web中生成验证码的知识用上。
于是,就写了这个。
分为两个文件:<CheckCode.java> 和 <CheckCodeRead.java> ,前者是生成,后者是读取,均可以在控制台打印验证码值 checkValue 。
CheckCode
package view;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;
/**
* @Classname CheckCode
* @Description TODO
* @Date 2020/3/31 下午 5:13
* @Created by jerry
*/
public class CheckCode {
public void creatCheck() {
char[] chs = new char[4];
int width = 100;
int height = 50;
//1.创建一对象,在内存中图片(验证码图片对象)
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//2.美化图片
//2.1 填充背景色
Graphics g = image.getGraphics();//画笔对象
g.setColor(Color.PINK);//设置画笔颜色
g.fillRect(0,0,width,height);
//2.2画边框
g.setColor(Color.BLUE);
g.drawRect(0,0,width - 1,height - 1);
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789";
//生成随机角标
Random ran = new Random();
StringBuilder sb = new StringBuilder(); // StringBuilder:一个可变的字符序列
for (int i = 1; i <= 4; i++) {
int index = ran.nextInt(str.length());
//获取字符
char ch = str.charAt(index);//随机字符
sb.append(ch);
chs[i-1] = ch;
//2.3写验证码
g.drawString(ch+"",width/5*i,height/2);
}
String checkCode_session = sb.toString();
/* //将验证码存入session
request.getSession().setAttribute("checkCode_session",checkCode_session);*/
//2.4画干扰线
g.setColor(Color.GREEN);
//随机生成坐标点
for (int i = 0; i < 10; i++) {
int x1 = ran.nextInt(width);
int x2 = ran.nextInt(width);
int y1 = ran.nextInt(height);
int y2 = ran.nextInt(height);
g.drawLine(x1,y1,x2,y2);
}
//3.将图片、验证码值写入硬盘
// ImageIO.write(image,"jpg",response.getOutputStream());
try {
String encoding="GBK";
ImageIO.write(image, "png",new File("F:\\MyJava\\LibrarySystem\\v0.0\\src\\image\\check.png"));
File file = new File("F:\\MyJava\\LibrarySystem\\v0.0\\src\\image\\check.txt");
Writer out = new OutputStreamWriter(new FileOutputStream(file),encoding);
out.write(checkCode_session); // 将验证码值写入check.txt文件里
out.close(); // 关闭输出流,回收资源
System.out.println(checkCode_session);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new CheckCode().creatCheck();
}
}
CheckCodeRead
package view;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/**
* @Classname CheckCodeCreate
* @Description TODO
* @Date 2020/3/31 下午 5:51
* @Created by jerry
*/
public class CheckCodeRead {
/**
* 功能:Java读取txt文件的内容
* 步骤:1:先获得文件句柄
* 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
* 3:读取到输入流后,需要读取生成字节流
* 4:一行一行的输出。readline()。
* 备注:需要考虑的是异常情况
* @param filePath
*/
public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
public CheckCodeRead() {
String filePath = "F:\\MyJava\\LibrarySystem\\v0.0\\src\\image\\check.txt";
// "res/";
readTxtFile(filePath);
}
public static void main(String argv[]){
new CheckCodeRead();
}
}
效果
验证码图片 check.png 👇
控制台输出 👇
check.txt 保存 验证码值 👇
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!