@WebServlet("/captche")
public class CaptcheController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(generatepic(100,80),"jpeg",sos);
sos.flush();
sos.close();
}
private BufferedImage generatepic(int width,int height){
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(100,100,width,height);
g.setFont(new Font("宋体",Font.BOLD, 20));
Random rnd =new Random();
Color color = new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256));
g.setColor(color);
g.drawString("1234",20,20);
int p= rnd.nextInt(100);
return image;
}
}