如图:
代码:
ByteArrayOutputStream jpgout = new ByteArrayOutputStream(); String id = req.getSession().getId(); BufferedImage bimg = new DefaultManageableImageCaptchaService().getImageChallengeForID(id,req.getLocale()); JPEGImageEncoder imgcode = JPEGCodec.createJPEGEncoder(jpgout); imgcode.encode(bimg); byte[] captchaChallengeAsJpeg = jpgout.toByteArray(); resp.setHeader("Cache-Control", "no-store"); resp.setHeader("Pragma", "no-cache"); resp.setDateHeader("Expires", 0); resp.setContentType("image/jpeg"); ServletOutputStream responseOutputStream = resp.getOutputStream(); responseOutputStream.write(captchaChallengeAsJpeg); responseOutputStream.flush(); responseOutputStream.close();