java图片验证码

servlet:
package st;
import java.awt.Color;
import java.awt.Font;
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.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
10.10.0.10
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class RandomCode
*/
public class RandomCode extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
冷却塔减震器throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 验证码图片的宽度。
int width = 70;
// 验证码图片的高度。
int height = 30;
BufferedImage buffImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = ateGraphics();
/
/ 创建一个随机数生成器类。
Random random = new Random();
// 设定图像背景(因为是做背景,所以偏淡)
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
// 创建字体,字体的大小应该根据图片的高度来定。
Font font = new Font("Times New Roman", Font.HANGING_BASELINE, 28);
// 设置字体。
g.setFont(font);
// 画边框。
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到。
//g.setColor(Color.GRAY);
g.setColor(getRandColor(160,200));
for (int i = 0; i < 155; i++) {
int x = Int(width);
int y = Int(height);
int xl = Int(12);
int yl = Int(12);
g.drawLine(x, y, x + xl, y + yl);
}
/
按钮指示灯/ randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBuffer randomCode = new StringBuffer();
// 设置默认生成4个验证码
int length = 4;
// 设置备选验证码:包括"a-z"和数字"0-9"
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int size = base.length();
// 随机产生4位数字的验证码。
for (int i = 0; i < length; i++) {
// 得到随机
产生的验证码数字。
int start = Int(size);
String strRand = base.substring(start, start + 1);
// 用随机产生的颜将验证码绘制到图像中。
// 生成随机颜(因为是做前景,所以偏深)
//g.setColor(getRandColor(1, 100));
//调用函数出来的颜相同,可能是因为种子太接近,所以只能直接生成
g.setColor(new Color(Int(110),Int(110),Int(110)));
g.drawString(strRand, 15 * i + 6, 24);
// 将产生的四个随机数组合在一起。
randomCode.append(strRand);
}
/
/ 将四位数字的验证码保存到Session中。
HttpSession session = Session();
session.setAttribute("rand", String());
//图象生效
g.dispose();
// 禁止图像缓存。
response.setHeader("Pragma", "no-cache");
电极扁钢response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
// 将图像输出到Servlet输出流中。
ServletOutputStream sos = OutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.flush();
sos.close();
}
Color getRandColor(int fc, int bc) {// 给定范围获得随机颜 模块化机组
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + Int(bc - fc);
int g = fc + Int(bc - fc);
int b = fc + Int(bc - fc);
return new Color(r, g, b);
}
}
<servlet>
<description></description>
<display-name>RandomCode</display-name>
<servlet-name>RandomCode</servlet-name>
<servlet-class>st.RandomCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RandomCode</servlet-name>
<url-pattern>/RandomCode</url-pattern>
</servlet-mapping>
html:
<body>
<script type="text/javascript">
/**刷新iframe**/
function refreshCode(){
window.frames["codeFrame"].load();
药盒}
/**替换图片,经测试在firefox下必须使用Math.random();**/
function reloadCode(){
marginheight="0" scrolling="no" allowtransparency="yes" height="35" width="102"></iframe>
<a href="#" onclick="refreshCode();">看不清,换一张</a>
<br>
<span id="codeImg"><img bord
er=0 src="127.0.0.1/js_test/RandomCode"></span>
<a href="#" onclick="reloadCode();">看不清,再换一张</a>
</body>

本文发布于:2024-09-23 07:20:21,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/219072.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:验证码   颜色   产生   图片   图像   输出   字体
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议