当前位置: 首页 > news >正文

java图片验证码

第一步:创建一个Servlet文件
//数字随机码
public class generateCode extends HttpServlet {
private static final long serialVersionUID = 5039673797977591965L;

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
processRequest(req, resp);
}

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
processRequest(req, resp);
}

public void processRequest(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.setContentType("11.gif"); //要设定好一个图片
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
HttpSession session = request.getSession();
int width = 80, height = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);

g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}

String sRand = "";
for (int i = 0; i < 6; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(rand, 13 * i + 6, 16);
}
session.setAttribute("rand", sRand);
g.dispose();
ServletOutputStream responseOutputStream = response.getOutputStream();
ImageIO.write(image, "JPEG", responseOutputStream);
responseOutputStream.flush();
responseOutputStream.close();
}

Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
}

第二步 设置web.xml

        <servlet>
<servlet-name>showImgCode</servlet-name>
<servlet-class>generateCode </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>showImgCode</servlet-name>
<url-pattern>/11.GIF</url-pattern>
</servlet-mapping>

第三步 创建相应的JSP
index.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>认证码输入页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">

<script language="JavaScript">
          function myrefresh()
          {
            window.location.reload();
          }
      </script>
</head>
<body>
<form method=post action="check.jsp">
<table>
<tr>
<td align=left>
系统产生的认证码:
</td>
<td>
<img border=0 src="11.GIF">
<input type="button" value="看不清,在换一张" οnclick="myrefresh()">
</td>
</tr>
<tr>
<td align=left>
输入上面的认证码:
</td>
<td>
<input type=text name=rand maxlength=6 value="">
</td>
</tr>
<tr>
<td colspan=2 align=center>
<input type=submit value="提交检测">
</td>
</tr>
</table>
</form>
</body>
</html>

Check.jsp
<%@ page contentType="text/html; charset=gb2312" language="java"
errorPage=""%>
<html>
<head>
<title>认证码验证页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
</head>
<body>
<%
String rand = (String) session.getAttribute("rand");
String input = request.getParameter("rand");
%>
系统产生的认证码为:
<%=rand%><br>
您输入的认证码为:
<%=input%><br>
<br>
<%
if (rand.equals(input)) {
%>
<font color=green>输入相同,认证成功!</font>
<%
} else {
%>
<font color=red>输入不同,认证失败!</font>
<%
}
%>
</body>
</html>
==================
长生字符和数据的验证码


public class CharactorCode extends HttpServlet {
private static final long serialVersionUID = 4347521091342882490L;
private Font mFont = new Font("Arial Black", Font.PLAIN, 16);

public void init() throws ServletException {
super.init();
}

Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}

public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("11.gif");

int width = 100, height = 18;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(1, 1, width - 1, height - 1);
g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, width - 1, height - 1);
g.setFont(mFont);

g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x, y, x + xl, y + yl);
}
for (int i = 0; i < 70; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x, y, x - xl, y - yl);
}

String sRand = "";
for (int i = 0; i < 6; i++) {
String tmp = getRandomChar();
sRand += tmp;
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(tmp, 15 * i + 10, 15);
}

HttpSession session = request.getSession(true);
session.setAttribute("rand", sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
}

private String getRandomChar() {
int rand = (int) Math.round(Math.random() * 2);
long itmp = 0;
char ctmp = '/u0000';
switch (rand) {
case 1:
itmp = Math.round(Math.random() * 25 + 65);
ctmp = (char) itmp;
return String.valueOf(ctmp);
case 2:
itmp = Math.round(Math.random() * 25 + 97);
ctmp = (char) itmp;
return String.valueOf(ctmp);
default:
itmp = Math.round(Math.random() * 9);
return String.valueOf(itmp);
}
}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • copyEvens
  • 字符设备驱动程序之异步通知
  • HTTP 错误 403.9 - 禁止访问:连接的用户过多
  • 如何使用flex皮肤
  • 查看进程占用的文件和文件数目
  • 动态创建DataGrid 列
  • Vue.js基础入门
  • 在 Tree 中查找节点
  • jQery使网页在显示器上居中显示适用于任何分辨率
  • 动态显示/隐藏 DataGrid 的列
  • Plugin with id 'com.github.dcendents.android-maven' not found
  • Flex构建WebService应用
  • Python 接口测试(五)
  • url中文乱码解决大全
  • warning: unable to bind to property '' on class '' (class is not an IEventDispatcher)
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • Android系统模拟器绘制实现概述
  • es6(二):字符串的扩展
  • Js基础——数据类型之Null和Undefined
  • node学习系列之简单文件上传
  • passportjs 源码分析
  • PAT A1017 优先队列
  • seaborn 安装成功 + ImportError: DLL load failed: 找不到指定的模块 问题解决
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • 记一次和乔布斯合作最难忘的经历
  • 紧急通知:《观止-微软》请在经管柜购买!
  • 警报:线上事故之CountDownLatch的威力
  • 浏览器缓存机制分析
  • 如何设计一个比特币钱包服务
  • 入门级的git使用指北
  • 实现菜单下拉伸展折叠效果demo
  • 实战|智能家居行业移动应用性能分析
  • 手写双向链表LinkedList的几个常用功能
  • elasticsearch-head插件安装
  • linux 淘宝开源监控工具tsar
  • ​你们这样子,耽误我的工作进度怎么办?
  • ‌分布式计算技术与复杂算法优化:‌现代数据处理的基石
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • #pragma pack(1)
  • $LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
  • (26)4.7 字符函数和字符串函数
  • (二)十分简易快速 自己训练样本 opencv级联lbp分类器 车牌识别
  • (黑客游戏)HackTheGame1.21 过关攻略
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (十)DDRC架构组成、效率Efficiency及功能实现
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (循环依赖问题)学习spring的第九天
  • (转)c++ std::pair 与 std::make
  • (转)scrum常见工具列表
  • (轉貼) 蒼井そら挑戰筋肉擂台 (Misc)
  • .bat批处理出现中文乱码的情况
  • .Net core 6.0 升8.0
  • .net core 外观者设计模式 实现,多种支付选择
  • .NET程序集编辑器/调试器 dnSpy 使用介绍
  • .Net语言中的StringBuilder:入门到精通