package com.oracle;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
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;
@WebServlet("/random")
public class RandomImg extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置给浏览器响应的类型
resp.setContentType("image/png");
ServletOutputStream os = resp.getOutputStream();
//一个缓存区
BufferedImage image=new BufferedImage(120, 40, BufferedImage.TYPE_INT_RGB);
Graphics gs = image.getGraphics();
gs.setColor(new Color(230,230,230));
gs.fillRect(0, 0, 120, 40);
//随机画50条线
Random random=new Random();
for(int i=0;i<50;i++)
{
gs.setColor(new Color(200+random.nextInt(30),100+random.nextInt(30),200+random.nextInt(30)));
gs.drawLine(random.nextInt(120),random.nextInt(40), random.nextInt(120), random.nextInt(40));
}
//循环写出4个随机字母,并将它们存在stringBuilder,并且写在session上
StringBuilder sBuffer=new StringBuilder();
for(int i=0;i<4;i++)
{
String string=this.randChar();
sBuffer.append(string);
gs.setColor(new Color(160+random.nextInt(30),80+random.nextInt(30),80+random.nextInt(30)));
gs.setFont(new Font("微软雅黑", Font.BOLD, 26));
gs.drawString(string,16+i*30, 23+random.nextInt(5));
}
//必须用tostring
//不然就是这个java.lang.StringBuilder cannot be cast to java.lang.String
req.getSession().setAttribute("rand", sBuffer.toString());
//将图片写到输出流
ImageIO.write(image, "png", os);
}
//生成一个随机字母
public String randChar() {
int re=(int) ((Math.random()*26)+65);
return String.valueOf((char)re);
}
}
图形验码器
最新推荐文章于 2022-12-21 13:40:50 发布