caching images served by servlet

后端 未结 4 1364
臣服心动
臣服心动 2020-12-03 01:44

I\'m serving up images from my servlet. The response content type is image/jpeg. I find that images requested from my servlet are not cached. How do I get them to be cached

4条回答
  •  时光说笑
    2020-12-03 02:26

    You need to send the ETag, Last-Modified and Expires headers along the response. The ETag represents the unique identifier of the file (usually composed based on a combination of filename, filesize and lastmodified timestamp). The Last-Modified represents the last modified timestamp of the file. The Expires header denotes how long the client is allowed to keep the file in cache. If the cache has been expired and the ETag or Last-Modified are available, then the client will send a HEAD request to check if the file needs to be renewed. If not, then the Expires will just be postponed again accordingly.

    You can find here a servlet example which handles this all (and download resumes and automatic GZIP): FileServlet supporting resume and GZIP

提交回复
热议问题