Apache Tomcat MimeTypes - 有什么方法可以获取它们吗?

发布于 2024-12-08 15:01:41 字数 127 浏览 0 评论 0原文

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

I'm writing a filter for Apache Tomcat, I was wondering if there's a way to fetch the mimetypes placed in the /conf/web.xml file configuration file without reading the xml file explicitly. Is there anything available in the Apache Tomcat libraries perhaps?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流殇 2024-12-15 15:01:41

来自 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 类型映射。如果你真的需要
你可以通过这个丑陋的 hack 获得扩展的完整列表:

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>

From the 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.                                                        -->

So they are available through the ServletContext.getMimeType method:

@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");
    ...
}

I haven't found any other public API for getting the whole MIME type mapping. If you really need
it you can get the complete list of the extensions with this ugly hack:

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);
}

It works on Tomcat 7.0.21. Since it uses Tomcat's internal classes there is no guarantee that it will work with other Tomcat versions.

Note that you still need to call the ServletContext.getMimeType to get the MIME types.

The required maven dependency:

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-catalina</artifactId>
    <version>7.0.21</version>
    <scope>provided</scope>
</dependency>
天荒地未老 2024-12-15 15:01:41

为什么不直接这样做:

        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()

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

: com/apache/tomcat/blob/master/java/org/apache/catalina/startup/MimeTypeMappings.properties" rel="nofollow noreferrer">https://github.com/apache/tomcat/blob/master/java/org/apache/catalina/startup/MimeTypeMappings.properties

这样你就可以获得特定 mime 类型的所有文件扩展名。

Why not just do this:

        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
                }
            }
        }

Straight from org.apache.catalina.startup.Tomcat.addDefaultMimeTypeMappings()

If you look at the properties file, it has everything I think you'd need:

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

This way you can get all the file extensions for a specific mime type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文