如何配置Tomcat静态服务的文件扩展名

3
在托管在Tomcat 6服务器上的现有Java Web应用程序中,我注意到所有以特定扩展名结尾的URL(如.png.gif)都是静态提供的,而如果将扩展名替换为更奇异的名称,例如.eot,则由Servlet处理。
这个文件扩展名列表在哪里配置?我检查了web.xmlserver.xml,但没有找到任何有关pnggif的提及。
3个回答

4
只要没有定义servlet(-mapping),所有文件都会以静态方式提供服务。mime扩展名在Tomcat的默认web.xml文件夹/conf中预定义。
<!-- ===================== Default MIME Type Mappings =================== -->
<!-- When serving static resources, Tomcat will automatically generate    -->
<!-- a "Content-Type" header based on the resource's filename extension,  -->
<!-- based on these mappings.  Additional mappings can be added here (to  -->
<!-- apply to all web applications), or in your own application's web.xml -->
<!-- deployment descriptor.                                               -->

<mime-mapping>
    <extension>123</extension>
    <mime-type>application/vnd.lotus-1-2-3</mime-type>
</mime-mapping>
...

已经为“eot”扩展名设置了MIME映射:<mime-mapping><extension>eot</extension><mime-type>application/vnd.ms-fontobject</mime-type></mime-mapping>。那么为什么它不能像其他静态资源一样被提供呢? - sdabet
它是如何提供的?我猜你的意思不是像下载那样,而是在浏览器中打开? - Stefan

2
除了 @Stefoan 提供的最优解决方案外,Tomcat AS 还定义了映射到名称为 defaultDefaultServlet,可提供所有静态资源(jpg、html、css、gif 等)的服务。您可以在此处找到其文档。(文档链接) 因此,您只需要添加一个指向它的servlet映射,即可将一些自定义文件扩展名作为静态资源进行服务。
<servlet-mapping>   
  <servlet-name>default</servlet-name>
  <url-pattern>*.eot</url-pattern>
</servlet-mapping>

2
事实上,我的问题特定于Hippo CMS / Bloomreach Experience配置(用于我的项目)。它需要将.eot添加到/hst:hst/hst:hosts节点下的hst:suffixexclusions属性列表中,如HippoCMS - Request Handling - Mount Matching所述。您还可以将排除规则添加到site/src/main/webapp/WEB-INF/hst-config.properties文件中。请注意,保留HTML标签。
filter.prefix.exclusions = /ping/, /fonts/
filter.suffix.exclusions = .eot, .woff, .woff2

这将解决Tomcat返回大多数字体文件的HTTP 404错误代码的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接