java 缓存由 servlet 提供的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2872613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:13:43  来源:igfitidea点击:

caching images served by servlet

javahttpimageservletscaching

提问by akula1001

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 like file image requests normally are? I tried setting Cache-Control: public but to no avail.

我正在从我的 servlet 提供图像。响应内容类型是图像/jpeg。我发现从我的 servlet 请求的图像没有被缓存。如何让它们像通常的文件图像请求一样被缓存?我尝试设置 Cache-Control: public 但无济于事。

采纳答案by akula1001

Ok.. looks like the default header fields should enable caching. I found a solution in another forum. Apparently, you need to explicitly set a content-length in the response. Wonder why though. I thought the HttpServletResponse would do that for us. Anyway, it worked like a charm and the image is getting cached nicely.

好的.. 看起来默认头字段应该启用缓存。我在另一个论坛找到了解决方案。显然,您需要在响应中明确设置内容长度。不知道为什么。我认为 HttpServletResponse 会为我们做到这一点。无论如何,它就像一个魅力,图像被很好地缓存。

回答by ZZ Coder

The default servlet serving static content in containers like Tomcat doesn't set any cache control headers. You don't need write a servlet just for that. Just create a filter like this,

在 Tomcat 等容器中提供静态内容的默认 servlet 不会设置任何缓存控制标头。您不需要为此编写 servlet。只需创建一个这样的过滤器,

public void doFilter(ServletRequest request,
        ServletResponse response,
        FilterChain chain) 
    throws IOException, ServletException {

    long expiry = new Date().getTime() + cacheAge*1000;

    HttpServletResponse httpResponse = (HttpServletResponse)response;
    httpResponse.setDateHeader("Expires", expiry);
    httpResponse.setHeader("Cache-Control", "max-age="+ cacheAge);

    chain.doFilter(request, response);

 }

Whenever you want add cache control, just add the filter to the resources in web.xml. For example,

每当您想要添加缓存控制时,只需将过滤器添加到 web.xml 中的资源即可。例如,

<filter>
    <filter-name>CacheControl</filter-name>
    <filter-class>filters.CacheControlFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>CacheControl</filter-name>
    <url-pattern>/images/*</url-pattern>
</filter-mapping>

回答by BalusC

You need to send the ETag, Last-Modifiedand Expiresheaders along the response. The ETagrepresents the unique identifier of the file (usually composed based on a combination of filename, filesize and lastmodified timestamp). The Last-Modifiedrepresents the last modified timestamp of the file. The Expiresheader denotes how long the client is allowed to keep the file in cache. If the cache has been expired and the ETagor Last-Modifiedare available, then the client will send a HEADrequest to check if the file needs to be renewed. If not, then the Expireswill just be postponed again accordingly.

您需要在响应中发送ETag,Last-ModifiedExpires标头。的ETag表示文件(基于文件名,文件大小和上次更改时间时间戳的组合通常由)的唯一标识符。该Last-Modified代表文件的最后修改时间戳。该Expires头表示允许客户端保留多久文件中缓存。如果缓存已经过期,ETagLast-Modified可用,则客户端会发送一个HEAD以检查是否续期的文件需要请求。如果没有,那么Expires将相应地再次推迟。

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

您可以在此处找到处理这一切的 servlet 示例(并下载简历和自动 GZIP):FileServlet 支持简历和 GZIP

回答by Bozho

For example, if you want to cache them for 1 month:

例如,如果您想将它们缓存 1 个月:

Calendar inOneMonth = Calendar.getInstance();
inOneMonth.add(Calendar.MONTH, 1);

response.setDateHeader("Expires", inOneMonth.getTimeInMillis());

(this is in a Filterthat handles the *.jpgpattern, for example)

(例如,这是在Filter处理*.jpg模式的a中)

But images should be cached by default - check your filters and configurations to see if something isn't setting the cache parameters incorrectly.

但是默认情况下图像应该被缓存——检查你的过滤器和配置,看看是否有什么东西没有错误地设置缓存参数。