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的基 ...
随机推荐
- 【原创】Proxmark3系列教程1——PM3用法
1 PM3介绍 proxmark3是一款开源的RFID安全研究平台黑色按钮从图中我们可以看到左上方有一颗黑色按钮,这个按钮就是Proxmark3的功能键,主要用于启动嗅探模式以及停止进程功能,其中内置 ...
- JAVA高级-面试题总结
最近面试了一些公司,针对面试中遇到的问题在此记录,提升自己,造福大家 一.java源码相关 ArrayList创建和add等各种api使用原理 HashMap 的创建,put原理,和HashTable ...
- Python 锁 同步 互斥锁
import time from threading import Lock,Thread num = 100 def f1(loc): loc.acquire() global num tmp = ...
- Android大作业
1.项目成员 邓乾尧 学号:1600802005 班级:161 博客:http://www.cnblogs.com/2575590018dqy/ 韦家城 学号:1600802026 班级:161 ...
- oracle 数据库中某个字段逗号分隔,得到对应列中的个数(列转行)实现方法
由于各种原因,数据的原则问题,导致某个字段上出现多个数据(依据分隔符隔开),比如 name 字段为 张三;李四;王五等等 需求:求一张表中name字段中出现的个数: 想要得到的结果为: 对应的sql语 ...
- 监测NGINX服务的shell脚本
Nginx 虽然处理并发量比 apache 确实要强点,但它这种 php-cgi 模式不是太稳定,这点网上也有朋友总结了,我在实现项目中也感受到了. 偶尔会出现以下情况的:php-cgi 进程突然消失 ...
- SpringBoot的启动流程分析(1)
通过分析我们可以找到 org.springframework.boot.SpringApplication 中如下, public static ConfigurableApplicationCont ...
- matlab中如何用rand产生相同的随机数
直接给链接:rand()产生相同随机数
- s21day22 python笔记
s21day22 python笔记 一.内容回顾及补充 模块补充 importlib.import_module:通过字符串的形式导入模块 #示例一: import importlib # 用字符串的 ...
- 田螺便利店—filezilla实现Linux和windows通信(二)
filezilla,FileZilla是一个免费开源的FTP软件,分为客户端版本和服务器版本,具备所有的FTP软件功能.可控性.有条理的界面和管理多站点的简化方式使得Filezilla客户端版成为一个 ...