java Servlet生成随机验证码
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
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.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class ResponseDemo2
*/
@WebServlet("/ResponseDemo2")
public class ResponseDemo2 extends HttpServlet {
private static final long serialVersionUID = 1L; private static int imageHeight = 50;
private static int imageWidth = 110; /**
* @see HttpServlet#HttpServlet()
*/
public ResponseDemo2() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
BufferedImage bufferImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = bufferImage.createGraphics(); // 1.设置背景色
SetBackGroundColor(g);
// 2.设置边框
SetBorder(g);
// 3.画干扰线
DrawRandomLine(g);
// 4.写随机数
DrawRandomNum((Graphics2D) g);
// 输出给浏览器
response.setContentType("image/jpeg");
ImageIO.write(bufferImage, "jpg", response.getOutputStream());
} private void DrawRandomNum(Graphics2D g) {
// TODO Auto-generated method stub
g.setColor(Color.RED);
g.setFont(new Font("宋体", Font.PLAIN, 40));
String baseString = "1234567890";
for (int i = 0; i < 4; i++) {
int x = 5 + i * 25; double degree = (new Random().nextInt()%30) * Math.PI / 180;
g.rotate(degree, x, 25);
g.drawString(baseString.charAt(new Random().nextInt(baseString.length())) + "", x, 40);
g.rotate(-degree, x, 25);
}
} private void DrawRandomLine(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.RED);
for (int i = 0; i < 20; i++) {
int x1 = new Random().nextInt(imageWidth);
int y1 = new Random().nextInt(imageHeight);
int x2 = new Random().nextInt(imageWidth);
int y2 = new Random().nextInt(imageHeight);
g.drawLine(x1, y1, x2, y2);
}
} private void SetBorder(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.BLUE);
g.drawRect(1, 1, imageWidth - 2, imageHeight - 2);
} private void SetBackGroundColor(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.WHITE);
g.fillRect(0, 0, imageWidth, imageHeight);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
java Servlet生成随机验证码的更多相关文章
- servlet生成随机验证码
package com.cgyue; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import jav ...
- Java生成随机验证码
package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- 【Java】生成图形验证码
本章介绍一个能生成比较好看的图形验证码类 生成验证码工具类 package com.util; import java.awt.Color; import java.awt.Font; import ...
- struts2生成随机验证码图片
之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...
- Python 生成随机验证码
Python生成随机验证码 Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...
- Python生成随机验证码
Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...
- Python使用PIL模块生成随机验证码
PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...
- C#生成随机验证码例子
C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...
- pillow实例 | 生成随机验证码
1 PIL(Python Image Library) PIL是Python进行基本图片处理的package,囊括了诸如图片的剪裁.缩放.写入文字等功能.现在,我便以生成随机验证码为例,讲述PIL的基 ...
随机推荐
- 网络协议中HTTP,TCP,UDP,Socket,WebSocket的优缺点/区别
先说一下网络的层级:由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层 1.TCP和UDP TCP:是面向连接的一种传输控制协议.属于传输层协议.TCP连接之后,客户端和服务器可 ...
- python日常小计
1.查看变量类型: pring type(item) 2.解决list中的中文显示乱码 使用decode('string_escap')将数据库查询返回的将带转义的字节码字符串转换为成utf-8中文
- Mybatis-基于配置文件的配置(——纪念这个即将被抛弃的孩子)
虽然内心相信Mybatis基于配置文件的配置早已经在实战之中被注解所遗忘,但是我相信还是会有一小部分人还是需要这种技术去维护原有使用这种方式去搭建的项目. 废话不多说首先使用框架包是不能少的了.导入M ...
- 学习笔记--python中使用多进程、多线程加速文本预处理
一.任务描述 最近尝试自行构建skip-gram模型训练word2vec词向量表.其中有一步需要统计各词汇的出现频率,截取出现频率最高的10000个词汇进行保留,形成常用词词典.对于这个问题,我建立了 ...
- iOS 10 Programming Fundamentals with Swift 学习笔记 0
1,a single statement can be broken into multiple lines ,For example, after an opening parenthesis is ...
- Xcode注释转文档appledoc使用
参考了一些大神的方法总算成功了记录一下少走弯路 1:安装appledoc 使用终端下载: 命令行: git clone git://github.com/tomaz/appledoc.git cd . ...
- 各组对本组——《BBW》软件开发意见的汇总
本来我们是想要做一款关于取快递的软件“快递来了”的. 但是因为我们班也有其他组在做类似的项目,并且根据我们的市场需求调查报告,87%的人都不会选择本软件让别人去取快递.所以经过我们讨论决定,我们修改了 ...
- mariadb-my.cnf
[client]port = 3306socket = /tmp/mysql.sockdefault-character-set=utf8 [mysqld]port = 3306socket = /t ...
- ValueError: attempted relative import beyond top-level package
python 项目 在pycharm中, 在某个文件夹下: 右键--> mark directory as --> source root 如何在python脚本或者shell中 用代码实 ...
- Go语言切片
切片 Go 语言切片相当于是对数组的抽象. 由于Go 数组的长度不可改变,在特定场景中这样的集合就不太适用,Go中提供了一种灵活,功能强悍的内置类型切片("动态数组"),与数组相比 ...