Apache Tomcat MimeTypes-以任何方式获取它们?

问题描述 投票:3回答:2

我正在为Apache Tomcat编写过滤器,我想知道是否有一种方法可以获取/conf/web.xml文件配置文件中的mimetypes,而无需显式读取xml文件。 Apache Tomcat库中可能有可用的东西吗?

jakarta-ee tomcat mime-types servlet-filters
2个回答
12
投票

tomcat/conf/web.xml

<!-- ======================== Introduction ============================== -->
<!-- This document defines default values for *all* web applications      -->
<!-- loaded into this instance of Tomcat.  As each application is         -->
<!-- deployed, this file is processed, followed by the                    -->
<!-- "/WEB-INF/web.xml" deployment descriptor from your own               -->
<!-- applications.                                                        -->

因此它们可以通过ServletContext.getMimeType方法使用:

@Override
protected void doGet(final HttpServletRequest req, 
        final HttpServletResponse resp) throws ServletException, IOException {
    final ServletContext servletContext = req.getServletContext();
    final String mimeType = servletContext.getMimeType("filename.txt");
    ...
}

我还没有找到任何其他公共API来获取整个MIME类型映射。如果你真的需要它可以通过此丑陋的技巧获得扩展的完整列表:

import java.util.Arrays;
import java.lang.reflect.Field;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.RequestFacade;
import org.apache.catalina.core.StandardContext;

...

// ugly reflection hack - do NOT use
final RequestFacade tomcatRequestFacade = (RequestFacade) req;
final Class<? extends RequestFacade> requestFacadeClass = 
    tomcatRequestFacade.getClass();
try {
    final Field field = requestFacadeClass.getDeclaredField("request");
    field.setAccessible(true);
    final Request tomcatRequest = (Request) field.get(tomcatRequestFacade);
    final StandardContext standardContext = 
        (StandardContext) tomcatRequest.getContext();
    final String[] mappings = standardContext.findMimeMappings();
    logger.info("mapping list: {}", Arrays.asList(mappings));
} catch (final Exception e) {
    logger.error("", e);
}

它适用于Tomcat 7.0.21。由于它使用Tomcat的内部类,因此不能保证它将与其他Tomcat版本一起使用。

注意,仍然需要调用ServletContext.getMimeType以获得MIME类型。

所需的Maven依赖项:

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-catalina</artifactId>
    <version>7.0.21</version>
    <scope>provided</scope>
</dependency>

0
投票

为什么不这样做:

        Properties defaultMimeMappings = new Properties();
        InputStream is = null;
        try {
            is = Tomcat.class.getResourceAsStream("MimeTypeMappings.properties");
            defaultMimeMappings.load(is);
            for (Map.Entry<Object, Object>  entry: defaultMimeMappings.entrySet()) {
                // context.addMimeMapping((String) entry.getKey(), (String) entry.getValue());
                // do what you need here or just the use the properties w/o this loop
            }
        } catch (IOException e) {
            throw new IllegalStateException(sm.getString("tomcat.defaultMimeTypeMappingsFail"), e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
        }

直接来自org.apache.catalina.startup.Tomcat.addDefaultMimeTypeMappings()

如果您查看属性文件,它包含了我认为您需要的一切:

https://github.com/apache/tomcat/blob/master/java/org/apache/catalina/startup/MimeTypeMappings.properties

© www.soinside.com 2019 - 2024. All rights reserved.