pageEncoding="UTF-8"%>
登陆页面function refresh() {
loginForm.image.src = "creatImage.jsp";
}
欢迎登陆本系统
账号:
密码:
验证码:
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,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));
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();
%>
图片生成相关文章
总结
以上是编程之家为你收集整理的java生成图片验证码示例程序全部内容,希望文章能够帮你解决java生成图片验证码示例程序所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250