博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java代码生成随机验证码
阅读量:3947 次
发布时间:2019-05-24

本文共 1608 字,大约阅读时间需要 5 分钟。

1.  新建一个controller

@Controller@RequestMapping("/index")public class LoginController {
@RequestMapping("/logins") public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
int width=100; int height=50; //创建一个对象,在内存中图片(验证码图片对象) BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //美化图片 //填充背景色 Graphics graphics = image.getGraphics(); graphics.setColor(Color.green); Font font = new Font("黑体",Font.PLAIN,20); graphics.setFont(font); graphics.fillRect(0,0,width,height); //画边框 graphics.setColor(Color.black); graphics.drawRect(0,0,width-1,height-1); String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; Random random = new Random(); for (int i = 0; i < 4; i++) {
int index = random.nextInt(str.length()); //随机字符 char charAt = str.charAt(index); //写验证码 graphics.drawString(charAt+"",width/5*i,height/2); } //画干扰线 graphics.setColor(Color.GREEN); //随机生成坐标点 for (int i = 0; i < 10; i++) {
int x1=random.nextInt(width); int x2= random.nextInt(width); int y1=random.nextInt(height); int y2=random.nextInt(height); graphics.drawLine(x1,y1,x2,y2); //将图片输出到页面上 ImageIO.write(image,"jpg",response.getOutputStream()); } }}

2.在resources/static目录下新建一个html文件

  
登录
用户名
密码
验证码

3.此项目的端口为8084,所以访问地址http://localhost:8084/login.html

在这里插入图片描述

转载地址:http://jnhwi.baihongyu.com/

你可能感兴趣的文章
计算机网络复习笔记
查看>>
boost学习-1.安装
查看>>
boost学习-2.总体感受
查看>>
boost学习-3.conversion,多态类型之间的安全转型,与数据类型转换
查看>>
2010年十大移动互联网应用将火山爆发
查看>>
云计算介绍
查看>>
敏捷开发笔记1
查看>>
vs2008
查看>>
转:NoSQL数据库探讨之一 - 为什么要用非关系数据库?
查看>>
log4cplus的按日生成文件,配置例子
查看>>
跨平台的文字编码转换方法--ICU
查看>>
ICU4C 4.4 静态库的编译
查看>>
FTP下载类, windows平台下对CFtpConnection上传下载的封装类
查看>>
代码自动生成-宏带来的奇技淫巧
查看>>
VC com开发中实现IObjectSafety
查看>>
c# 正则表达式基础
查看>>
C#3.0语言新特性
查看>>
W32Dasm反汇编工具使用教程
查看>>
EXE破解工具介绍
查看>>
机械码对应值
查看>>