random.jsp文件代码:
<%@ page autoFlush="false" import="java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*"%>
<%@ page import="org.apache.commons.lang.RandomStringUtils"%>
<%
RandomStringUtils rs=new RandomStringUtils();
String random=rs.randomAlphanumeric(4);
session.setAttribute("random",random);
%>
<%
out.clear();
response.setContentType("image/jpeg");
response.addHeader("pragma","NO-cache");
response.addHeader("Cache-Control","no-cache");
response.addDateHeader("Expries",0);
int width=100, height=40;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
//以下填充背景?色
g.setColor(Color.GRAY);
Font DeFont=new Font("SansSerif", Font.PLAIN, 32);
g.setFont(DeFont);
g.fillRect(0, 0, width, height);
//?置字体?色
g.setColor(Color.RED);
g.drawString(random,3,30);
g.dispose();
ServletOutputStream outStream = response.getOutputStream();
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(outStream);
encoder.encode(image);
outStream.close();
%>
test.jsp文件代码如下:
<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<img src="random.jsp">
</body>
</html>
如果不想导入Apache的包可以自己编译下面这个文件
// FrontEnd Plus GUI for JAD
// DeCompiled : RandomStringUtils.class
package org.apache.commons.lang;
import java.util.Random;
public class RandomStringUtils
{
private static final Random RANDOM = new Random();
public RandomStringUtils()
{
}
public static String random(int count)
{
return random(count, false, false);
}
public static String randomAscii(int count)
{
return random(count, 32, 127, false, false);
}
public static String randomAlphabetic(int count)
{
return random(count, true, false);
}
public static String randomAlphanumeric(int count)
{
return random(count, true, true);
}
public static String randomNumeric(int count)
{
return random(count, false, true);
}
public static String random(int count, boolean letters, boolean numbers)
{
return random(count, 0, 0, letters, numbers);
}
public static String random(int count, int start, int end, boolean letters, boolean numbers)
{
return random(count, start, end, letters, numbers, null);
}
public static String random(int count, int start, int end, boolean letters, boolean numbers, char set[])
{
if(start == 0 && end == 0)
{
end = 122;
start = 32;
if(!letters && !numbers)
{
start = 0;
end = 0x7fffffff;
}
}
StringBuffer buffer = new StringBuffer();
int gap = end - start;
while(count-- != 0)
{
char ch;
if(set == null)
ch = (char)(RANDOM.nextInt(gap) + start);
else
ch = set[RANDOM.nextInt(gap) + start];
if(letters && numbers && Character.isLetterOrDigit(ch) || letters && Character.isLetter(ch) || numbers && Character.isDigit(ch) || !letters && !numbers)
buffer.append(ch);
else
count++;
}
return buffer.toString();
}
public static String random(int count, String set)
{
return random(count, set.toCharArray());
}
public static String random(int count, char set[])
{
return random(count, 0, set.length - 1, false, false, set);
}
}
本文地址:http://com.8s8s.com/it/it11928.htm