准备一个验证码
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//禁用缓存
resp.setHeader("Pragma", "No-cache");
resp.setHeader("Cache-Control", "No-cache");
resp.setDateHeader("Expires", 0);
//根据自己的背景图片设置照片格式
resp.setContentType("image/jpg");
int height = 60;
int width = 140;
//画板
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//画笔
Graphics graphics = image.getGraphics();
//设置字体
Font font = new Font("宋体",Font.BOLD,25);
graphics.setFont(font);
//图像输入流,它是从当前已注册 ImageReader 中自动选择的,解码所提供 URL 的结果。
String imagePath = req.getServletContext().getRealPath("img/ba.jpg");
Image image1 = ImageIO.read(new File(imagePath));