最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java生成圖片驗(yàn)證碼示例程序

 更新時(shí)間:2013年11月29日 11:20:11   作者:  
這篇文章主要介紹了java生成圖片驗(yàn)證碼示例程序,大家參考使用吧

復(fù)制代碼 代碼如下:

<%@ 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>登陸頁(yè)面</title>
<script type="text/javascript">
 function refresh() {
  loginForm.image.src = "creatImage.jsp";
 }
</script>
</head>
<body>
<h1>歡迎登陸本系統(tǒng)</h1><br>
<form action="" method = "post" name="loginForm">
 <label>賬號(hào):<input name="username" type="text" /></label><br>
 <label>密碼:<input name="password" type="password" /></label><br>
 <label>驗(yàn)證碼:<input name="code" type="text" /></label>
 <!-- 將驗(yàn)證碼當(dāng)做圖片處理 -->
    <img name="image" border="0" src="creatImage.jsp" onclick="refresh()" />
 <input type="submit" value="登陸" />
</form>
</body>
</html>

復(fù)制代碼 代碼如下:

<%@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("");
 //產(chǎn)生四位數(shù)的字母數(shù)字驗(yàn)證碼,各個(gè)數(shù)字的顏色也隨即
 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);
 /*
 若是產(chǎn)生四位數(shù)字,則nextInt(8999) + 1000;
 然后String.valueOf轉(zhuǎn)換為String
 */
 //驗(yàn)證碼存入session里,方便在登陸校檢頁(yè)比對(duì)
 session.setAttribute("image",s);
 //輸出到頁(yè)面
 ImageIO.write(bi,"JPEG",response.getOutputStream());
 /*
 加入下面這兩句什么作用呢?
 否則報(bào)異常: 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>

相關(guān)文章

最新評(píng)論

旺苍县| 锡林郭勒盟| 郸城县| 壶关县| 奎屯市| 贵州省| 安化县| 东山县| 隆林| 庆城县| 醴陵市| 长子县| 无极县| 西华县| 屯留县| 烟台市| 五大连池市| 施秉县| 建德市| 南溪县| 东平县| 闵行区| 平阴县| 湛江市| 上思县| 山丹县| 炉霍县| 神池县| 红河县| 汽车| 万盛区| 浏阳市| 互助| 义马市| 乌拉特前旗| 上饶市| 望奎县| 彭水| 三门县| 南华县| 延寿县|