자바 이미지 인증 코드

9006 단어 자바
준비 지식
        어떤 자바 웹 책 에 도 있 습 니 다.그 몇 걸음 만 보면 됩 니 다.하지만 문제 가 있 을 수 있 습 니 다.지나 가 는 가르침 아래...
코드
<%@ 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>

좋은 웹페이지 즐겨찾기