生成servlet类,生成图片
package comluo;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* Servlet implementation class HttpTest
*/
@WebServlet("/HttpTest")
public class HttpTest extends HttpServlet {
private static final long serialVersionUID = 1L;
// 随机字符字典
public static final char[] CHARS={'2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h',
'j','k','m','n','p','q','r','s',
't','u','v','w','x','y','z'};
//随机数
public static Random random=new Random();
public static String getRandomString(){
StringBuffer buffer=new StringBuffer();
for(int i=0;i<6;i++){
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
}
//获取随机的颜色
public static Color getRandomColor(){
return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
}
//获取某颜色相反的颜色
public static Color getReverseColor(Color c){
return new Color(255-c.getRed(),255-c.getGreen(), 255-c.getBlue());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置输出类型
response.setContentType("image/jpeg");
//设置随机字符
String randomString=getRandomString();
//放入session中
request.getSession(true).setAttribute("randomString", randomString);
int height=30;int width=100;//图片高和宽
Color color=getRandomColor(); //获取随机颜色,用于前景色
Color reverse=getReverseColor(color);//获取反颜色,用于背景色
//创建一个彩色图片
BufferedImage bi=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
//获取绘图对象
Graphics2D g =bi.createGraphics();
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
g.setColor(color);//设置颜色
g.fillRect(0, 0, width, height);//设置背景
g.setColor(reverse);//设置颜色
//绘制随机字符
g.drawString(randomString, 18, 20);
//设置随机噪音点,最多一百个噪音点
for(int i=0,n=random.nextInt(100);i<n;i++){
g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
}
//转成JPEG格式
ServletOutputStream outputStream=response.getOutputStream();
//编码器
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(outputStream);
encoder.encode(bi);//对图片进行编码
outputStream.flush();//输出到客户端
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
index.jsp在网页输出:
<%@ 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>我的标题我做主</title>
</head>
<script type="text/javascript">
var i=0;
function changeImage(i) {
document.getElementById('btn').disabled=true;
document.getElementById('identity').src='HttpTest?ts='+i;
i++;
}
</script>
<body>
<img src="HttpTest" id="identity">
<input type="button" onclick="changeImage(i)" value="刷新图片" id="btn"/>
</body>
</html>
功能扩展:
添加input标签,后端接受用户输入验证码信息、校验