博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单验证码业务
阅读量:7056 次
发布时间:2019-06-28

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

1 //生成图片响应流,返回给客户端 2         //之前是text/html 3         //a.设置响应类型为图片 4         response.setContentType("image/jpeg"); 5         //b.创建出图片对象        BufferedImage(0, 0, 0):参数1图片宽度   参数2图片高度    参数3  图片的像素 6         BufferedImage image = new BufferedImage(70, 30, BufferedImage.TYPE_INT_RGB); 7         Graphics g = image.getGraphics();//获取该图片对象的画笔对象 8         //c.画背景  设置灰色背景 9         g.setColor(Color.GRAY);10         g.fillRect(0, 0, 70, 30);//通过填充矩形的方式,将整个画的内容填满11         //d.画字符串      12         //随机生成5位数的数字字符串13         Random r = new Random();14         int num = r.nextInt(100000-10000)+10000;  //10000~99999  15         String code =String.valueOf(num);//转换成字符串16         //将生成的字符串验证码放入session中 绑定到code属性上17         HttpSession session = request.getSession();18         session.setAttribute("code", code);19         20         //设置字体颜色,设置字体21         g.setColor(Color.BLUE);22         g.setFont(new Font("Consolas",Font.BOLD,20));23         //将字符串画在内存中的图片对象上   24         g.drawString(code, 5, 20);25         //画一些线条,增加破解难度26         g.setColor(Color.BLACK);27         g.drawLine(r.nextInt(70),r.nextInt(30),r.nextInt(70),r.nextInt(30));28         g.drawLine(r.nextInt(70),r.nextInt(30),r.nextInt(70),r.nextInt(30));29         g.drawLine(r.nextInt(70),r.nextInt(30),r.nextInt(70),r.nextInt(30));30         g.drawLine(r.nextInt(70),r.nextInt(30),r.nextInt(70),r.nextInt(30));31         32         //e.将图片对象通过流的方式响应回给页面33         //write(im, formatName, output):  im是图片对象     formatName 是设置图片的格式   output是输出流对象34         ImageIO.write(image, "JPEG", response.getOutputStream());

 

转载于:https://www.cnblogs.com/blackdeng/p/6060356.html

你可能感兴趣的文章
python 列表
查看>>
第七课作业
查看>>
MEAN实践——LAMP的新时代替代方案(下)
查看>>
CentOS7 下安装 Oracle 12c
查看>>
简单介绍AngularJs Filters
查看>>
Python中的dict
查看>>
Beautiful Soup4库文档学习
查看>>
理解cookie和session
查看>>
我的友情链接
查看>>
linux 5,6,9,12单元练习
查看>>
kill命令
查看>>
错题集06
查看>>
磁盘配额限制
查看>>
shell的bash特性
查看>>
HTML中的超链接
查看>>
什么是进程与线程
查看>>
nodeJS之URL
查看>>
网络配置
查看>>
使用Laya引擎开发微信小游戏(上)
查看>>
mycat 集群
查看>>