http://java.sun.com/products/java-media/jai/
1. random.jsp (产生四位的随机字符,由0-9,a-z,A-Z构成.并把最终字符串放到session中保存以让后续页面验证真伪)
代码如下:
<%@ page autoFlush="false" import="java.util.*,java.awt.*,java.awt.image.*,
com.sun.image.codec.jpeg.*,java.util.*"%
<%@ page import="" contentType="text/html; charset=gb2312"%
<%
String chose=
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char display[]={'0',' ','0',' ','0',' ','0'},ran[]={'0','0','0','0'},temp;
Random rand=new Random();
for(int i=0;i<4;i++)
{
temp=chose.charAt(rand.nextInt(chose.length()));
display[i*2]=temp;
ran[i]=temp;
}
String random=String.valueOf(display);
session.setAttribute("random",String.valueOf(ran));
%
<%
out.clear();
response.setContentType("image/jpeg");
response.addHeader("pragma","NO-cache");
response.addHeader("Cache-Control","no-cache");
response.addDateHeader("Expries",0);
int width=47, height=15;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
//以下填充背景颜色
g.setColor(Color.GREEN);
g.fillRect(0, 0, width, height);
//设置字体颜色
g.setColor(Color.RED);
g.drawString(random,3,10);
g.dispose();
ServletOutputStream outStream = response.getOutputStream();
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(outStream);
encoder.encode(image);
outStream.close();
%
本新闻共2页,当前在第1页 1 2
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/java/)