在 java 应用程序中仅在浏览器中缓存没有 .htaccess 文件的图像

Caching only images in browser without .htaccess file in java Application

这是java代码

WebResponse response = getWebRequestCycle().getWebResponse(); 
response.setHeader("Cache-Control", "max-age=31536000,must-revalidate"); 
response.setHeader("pragma", "max-age=31536000");

这里是html需要缓存图片的页面

<HTML>
<head></head>
<BODY>
<div><img src="images\banner1.png"/></div>
</BODY>
</HTML>

如果图像由 Wicket 管理(即它的 url 在最后一页看起来像 .../wicket/resource/com.example.MyComponent/some.img),那么您可以使用 Wicket 的 ResourceSettings 来配置默认缓存持续时间和缓存策略。

例如在 YourApplication#init() 做:getResourceSettings().setDefaultCacheDuration(Duration.days(180)).

如果图像不是由 Wicket 管理的,那么您应该使用 Servlet 过滤器,为您想要的所有静态资源添加这些响应 headers。 Tomcat 见 http://www.eclipse.org/jetty/documentation/current/advanced-extras.html for Jetty and https://tomcat.apache.org/tomcat-8.5-doc/config/filter.html#Expires_Filter