程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java生成圖片驗證碼示例法式

java生成圖片驗證碼示例法式

編輯:關於JAVA

java生成圖片驗證碼示例法式。本站提示廣大學習愛好者:(java生成圖片驗證碼示例法式)文章只能為提供參考,不一定能成為您想要的結果。以下是java生成圖片驗證碼示例法式正文



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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved