<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="java.awt.Font"%>
<%@page import="java.awt.Color"%>
<%@page import="java.awt.Graphics"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
response.setHeader("Expires", "0");
String oldChars="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";//验证码待选字符
String []codeChars = new String[5];
String saveCode="";
for(int i=0;i<5;i++){
int index=(int)(35*Math.random());
codeChars[i]=""+oldChars.charAt(index);
saveCode+=codeChars[i];
}
session.setAttribute("savedCheckCode",saveCode);
BufferedImage bi= new BufferedImage(80,20,BufferedImage.TYPE_USHORT_555_RGB);
Graphics gra=bi.getGraphics();
gra.setColor(Color.LIGHT_GRAY);
gra.fillRect(0, 0, 80, 20);
gra.setColor(Color.black);//设置字体颜色
Font font = new Font("Monotype Corsiva",Font.ITALIC,18);//设置字体
gra.setFont(font);
gra.drawString(codeChars[0], 1, 15);
gra.drawString(codeChars[1], 16, 13);
gra.drawString(codeChars[2], 31, 18);
gra.drawString(codeChars[3], 46, 14);
gra.drawString(codeChars[4], 61, 15);
gra.dispose();//释放资源,完成绘图
ByteArrayOutputStream image = new ByteArrayOutputStream();
ImageIO.write(bi,"jpeg",image);
byte[] imageByte = image.toByteArray();
response.setContentLength(imageByte.length);
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
sos.write(imageByte);
sos.flush();
image.close();
sos.close();
%>
</body>
</html>
夺
最新推荐文章于 2024-04-23 17:25:26 发布