国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java生成圖片驗證碼示例程序

2019-11-26 15:52:43
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

<%@ 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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 库尔勒市| 银川市| 霍城县| 金沙县| 丹阳市| 洪洞县| 容城县| 宁晋县| 平凉市| 宁阳县| 千阳县| 宁安市| 湖南省| 罗城| 墨江| 项城市| 玛多县| 什邡市| 弋阳县| 扬州市| 岗巴县| 松潘县| 平昌县| 寻乌县| 巴南区| 开鲁县| 柘荣县| 巴东县| 剑川县| 巴塘县| 湘乡市| 建湖县| 文化| 塔城市| 永昌县| 思南县| 达日县| 天门市| 景德镇市| 蕉岭县| 郧西县|