java生成图片验证码示例程序
时间:2023-09-13 10:17:57
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆页面</title>
<script type="text/javascript">
function refresh() {
loginForm.image.src = "creatImage.jsp";
}
</script>
</head>
<body>
<h1>欢迎登陆本系统</h1><br>
<form action="" method = "post" name="loginForm">
<label>账号:<input name="username" type="text" /></label><br>
<label>密码:<input name="password" type="password" /></label><br>
<label>验证码:<input name="code" type="text" /></label>
<!-- 将验证码当做图片处理 -->
<img name="image" border="0" src="creatImage.jsp" onclick="refresh()" />
<input type="submit" value="登陆" />
</form>
</body>
</html>
<%@page import="java.util.Random"%>
<%@page import="java.awt.Graphics"%>
<%@page import="javax.imageio.*"%>
<%@page import="java.awt.*"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
final char[] str = {'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
'r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int width=100,height=60;
BufferedImage bi = new BufferedImage(width,height,
BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(new Color(200,200,200));
g.fillRect(0, 0, width, height);
Random rnd = new Random();
StringBuffer sb = new StringBuffer("");
//产生四位数的字母数字验证码,各个数字的颜色也随即
for(int i=0; i<4; i++) {
int num = rnd.nextInt(str.length);
Color c = new Color(rnd.nextInt(256),
rnd.nextInt(256),rnd.nextInt(256));
g.setColor(c);
g.setFont(new Font("", Font.BOLD+Font.ITALIC, 20));
g.drawString(str[num]+"", 10, 17);
sb.append(str[num]);
}
//划干扰线
for(int i=0; i<10; i++) {
Color c = new Color(rnd.nextInt(256),
rnd.nextInt(256),rnd.nextInt(256));
g.setColor(c);
g.drawLine(rnd.nextInt(width), rnd.nextInt(height),
rnd.nextInt(width), rnd.nextInt(height));
}
String s = new String(sb);
/*
若是产生四位数字,则nextInt(8999) + 1000;
然后String.valueOf转换为String
*/
//验证码存入session里,方便在登陆校检页比对
session.setAttribute("image",s);
//输出到页面
ImageIO.write(bi,"JPEG",response.getOutputStream());
/*
加入下面这两句什么作用呢?
否则报异常: java.lang.IllegalStateException: getOutputStream()
has already been called for this response
不管原因了
*/
out.clear();
out = pageContext.pushBody();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图片生成</title>
</head>
<body>
</body>
</html>
标签:java,图片验证码
0
投稿
猜你喜欢
C# 三种序列化方法分享
2022-09-21 03:43:05
Maven profile实现不同环境的配置管理实践
2021-11-11 09:52:15
Java基础题新手练习(二)
2022-03-10 00:11:57
Java实现的计时器【秒表】功能示例
2023-11-10 21:24:50
C#算法之整数反转
2021-09-24 18:36:49
Android中使用Bitmap类将矩形图片转为圆形的方法
2022-01-04 18:47:40
C#实现闪动托盘图标效果的方法
2023-10-04 01:58:47
Java mybatis-plus详解
2023-03-28 19:49:04
Java SWT中常见弹出框实例总结
2023-08-22 00:42:22
Java毕业设计实战之二手书商城系统的实现
2022-03-21 14:06:20
C#创建一个Word并打开的方法
2022-08-04 23:33:13
浅谈Maven镜像更换为阿里云中央仓库(精)
2022-08-06 04:48:17
java基础二叉搜索树图文详解
2023-06-01 05:43:19
myBatis组件教程之缓存的实现与使用
2023-09-24 00:49:18
C语言中数据是如何存储在内存中的
2021-07-03 02:13:34
android TabLayout使用方法详解
2021-08-06 07:52:17
Java代码实现简单酒店管理系统
2023-11-21 03:38:57
如何利用java实现生成PDF文件
2023-03-31 16:25:36
Android自定义View实现波浪动画
2022-05-27 23:04:46
浅谈java实现背包算法(0-1背包问题)
2022-04-28 15:23:43