针对图片资源,根据http协议的缓存机制,进行了服务器更新处理,但遇到了个小问题,一直还没搞清楚的.见代码备注.
package com.g3net.servlet; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class PicServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (!Appcontext.isstarted) { return; } req.setCharacterEncoding("UTF-8"); resp.setContentType("image/png"); String key = req.getParameter("k"); long ifModifiedTime = req.getDateHeader("if-modified-since"); /* 打印输出 浏览器 返回的if-modified-since请求头数据,均返回 1388712683000l,而非 1388712683125l ,省略了毫秒级别数据.*/ System.out.println("ifModifiedSince:" + ifModifiedTime ); long lastModifyTime = PicCahceUtil.getLastModifyTime(key); /* 因此这里根据modifytime进行判断时,应省去毫秒级数据再进行对比*/ if(ifModifiedTime != -1 && ifModifiedTime != 0 && ifModifiedTime/1000 == lastModifyTime/1000){ resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return; } byte[] picdata = PicCahceUtil.getPicData(key); if (picdata != null && picdata.length > 1) { resp.setStatus(HttpServletResponse.SC_OK); /*便于测试,下发的"修改时间"为固定值 1388712683125l */ resp.addDateHeader("Last-Modified", 1388712683125l); resp.setHeader("Cache-Control", "max-age=" + 2);//表示浏览器最大缓存两天,两天内会根据modifytime进行更新处理; 超过两天则强制进行更新 resp.addDateHeader("Expires", System.currentTimeMillis() + 1000 * 120);//表示浏览器2分钟内不会再次请求服务器,以减少对服务器压力 /* 下发返回值 */ OutputStream out = resp.getOutputStream(); try { out.write(picdata); out.flush(); } catch (Exception e) { LogService.writeLog(LoggerNameType.FAILLOG, "下发图片数据出错key:" + key, LogService.ERROR); } finally { out.close(); } // 指标监控 Appcontext.dataCollection.addFunctionUsingNum("pic", 1); } else { resp.sendError(HttpServletResponse.SC_NOT_FOUND); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } public static void main(String[] args) { long value = 1388712683100l; System.out.println(Long.valueOf(value)); } }
相关推荐
主要介绍了HTTP缓存头Last-Modified和ETag介绍,需要的朋友可以参考下
NULL 博文链接:https://hyj1254.iteye.com/blog/1161000
----------------------------实现功能如下------------------------------------ 1.CAS-集成mysql 2.CAS-日志审计 3.CAS-连接池配置 4.CAS-自定义错误信息 5.CAS-识别json文件 6.CAS-页面缓存记住我 7.CAS-cookie...
HTTP 状态码 2xx:表示报文被成功接收 3xx:表示重定向,客户端...If-Modified-Since 和 Last-Modified:基于时间实现,在发现请求到的资源携带 Last-Modified,那 么在下一次请求时,会将其值携带在If-Modified-Since
修改last-modified的HTTP规范,实现了last-modified , if-modified-since , etag , if-none-match 。概要Modified是根据构建的,并通过缓存支持对其进行修饰,因此,如果您熟悉请求,则几乎可以使用修改了。 var...
redis-连接工具
ibatis缓存介绍 - 勇泽 - 博客园ibatis缓存介绍 - 勇泽 - 博客园ibatis缓存介绍 - 勇泽 - 博客园ibatis缓存介绍 - 勇泽 - 博客园
缓存 ehcache-1.3.0.rar 缓存 ehcache-1.3.0.rar 缓存 ehcache-1.3.0.rar 缓存 ehcache-1.3.0.rar
来自俄罗斯的异步图片加载缓存 Android-Universal-Image-Loader 图片缓存Android-Universal-Image-Loader-master
afinal框架实现图片的简单异步缓存加载.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
1. 概述 2. 缓存雪崩 3. 缓存击穿 4. 缓存穿透 5. 小结
强缓存通过这些头部直接决定是否使用缓存,而协商缓存则通过`Last-Modified`/`If-Modified-Since`或`ETag`/`If-None-Match`与服务器通信来验证资源是否更新。Service Worker提供了更细粒度的缓存控制,支持离线应用...
分布式缓存--缓存与数据库强一致场景下的方案.doc
浏览器之HTTP缓存的那些事 什么是浏览器缓存 浏览器HTTP执行机制 缓存模式 缓存相关的Http Header Cache-Control Expires Last-Modified ETag
浏览器缓存机制、Expires策略(http1.0)和Cache-control策略(http1.1)、Last-Modified/If-Modified-Since、ETag/If-None-Match
PHP禁止缓存如何禁止页面缓存,?... //设置此页面的过期时间(用格林威治时间表示),只要是已经过去的日期即可。 header("Expires: Mon, 26 Jul 1970 ...header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
前言设计一个缓存系统,不得不要考虑的问题就是:缓存穿透、缓存击穿与失效时的雪崩效应。缓存穿透缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并
memcached缓存服务-达内
R2M缓存介绍-业务使用方面