标签:get eth fill return err rand 浏览器缓存 Servle ted
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//使浏览器3秒自动刷新一次
resp.setHeader("refresh", "3");
//在内存中创建一个图片
BufferedImage image = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB);
//得到图片
Graphics2D graphics = (Graphics2D) image.getGraphics();//得到画笔
//设置图片的背景颜色
graphics.setColor(Color.blue);
graphics.fillRect(0, 0, 80, 20);
//给图片写数据
graphics.setColor(Color.white);
graphics.setFont(new Font(null,Font.BOLD,20));
graphics.drawString(makeNum(),0,20);
//告诉浏览器,这个请求用图片的方式打开
resp.setContentType("image/jpeg");
//网站存在缓存,不让浏览器缓存
resp.setDateHeader("expires",-1);
resp.setHeader("Cache-Control","no-cache");
resp.setHeader("Pragma","no-cache");
//把图片写给浏览器
ImageIO.write(image,"jpg",resp.getOutputStream());
}
private String makeNum() {
Random random = new Random();
String num = random.nextInt(99999) + "";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 7 - num.length(); i++) {
sb.append("0");
}
num = sb.toString() + num;
return num;
}
标签:get eth fill return err rand 浏览器缓存 Servle ted
原文地址:https://www.cnblogs.com/lsxsx/p/13378409.html