作者共发了2篇帖子。 内容转换:不转换▼
 
点击 回复
459 1
【程序】JSP验证码
一派护法 十九级
1楼 发表于:2016-9-16 18:41

<%@page import="java.awt.*, java.awt.image.*, java.util.*, javax.imageio.*" %><%!
// 函数只能在声明块中定义
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);
    Color clr = new Color(r, g, b);
    return clr;
}
%><%
response.setContentType("image/png");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);

int width = 75;
int height = 25;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 创建图像
Graphics graphics = image.getGraphics();
graphics.setColor(getRandColor(200, 250));
graphics.fillRect(0, 0, width, height); // 填充背景色
graphics.setColor(getRandColor(160, 200));
graphics.drawRect(0, 0, width - 1, height - 1); // 画边框

int i;
Random random = new Random();
graphics.setColor(getRandColor(160, 200));
for (i = 0; i < 155; i++) {
    int x = random.nextInt(width);
    int y = random.nextInt(height);
    int x1 = random.nextInt(12);
    int y1 = random.nextInt(12);
    graphics.drawLine(x, y, x + x1, y + y1);
}

// 取随机产生的认证码(4位数字)
String str = "";
Font font = new Font("Times New Roman", Font.PLAIN, 24);
graphics.setFont(font);
for (i = 0; i < 4; i++) {
    String rand = String.valueOf(random.nextInt(10));
    str += rand;
    Color clr = new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110));
    graphics.setColor(clr);
    graphics.drawString(rand, 13 * i + 14, 20);
}

session.setAttribute("vcode", str);
graphics.dispose();
ImageIO.write(image, "PNG", response.getOutputStream());
%>
一派护法 十九级
2楼 发表于:2016-9-16 18:41
本程序是根据:
http://mzba520.iteye.com/blog/990610
修改的

回复帖子

内容:
用户名: 您目前是匿名发表
验证码:
(快捷键:Ctrl+Enter)
 

本帖信息

点击数:459 回复数:1
评论数: ?
作者:巨大八爪鱼
最后回复:巨大八爪鱼
最后回复时间:2016-9-16 18:41
 
©2010-2024 Arslanbar Ver2.0
除非另有声明,本站采用知识共享署名-相同方式共享 3.0 Unported许可协议进行许可。