Wildfly暗潮文件MIMETYPES(Wildfly Undertow File Mimetyp

2019-09-25 18:54发布

我想暗潮服务像为.jpg,.png,.js文件,名为.css,.TXT等静态文件...

我编辑在standalone.xml暗流子系统:

<subsystem xmlns="urn:jboss:domain:undertow:4.0">
        <buffer-cache name="default"/>
        <server name="default-server">
            <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true"/>
            <https-listener name="https" socket-binding="https" security-realm="ApplicationRealm" enable-http2="true"/>
            <host name="default-host" alias="localhost">
                <location name="/images" handler="sh-resources"/>
                <filter-ref name="server-header"/>
                <filter-ref name="x-powered-by-header"/>
                <filter-ref name="content-png" predicate="path-suffix['.png']"/>
                <http-invoker security-realm="ApplicationRealm"/>
            </host>
        </server>
        <servlet-container name="default">
            <jsp-config/>
            <websockets/>
        </servlet-container>
        <handlers>
            <file name="sh-resources" path="/resource" directory-listing="true"/>
        </handlers>
        <filters>
            <response-header name="server-header" header-name="Server" header-value="WildFly/11"/>
            <response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/>
            <response-header name="content-png" header-name="Content-Type" header-value="image/png"/>
        </filters>
    </subsystem>

我甲肝在我的“/资源”文件夹“1.JPG”中,“2.png”,“js.js”,“c.css”一些文件:

HTTP://本地主机:8080 /资源/ 1.JPG - >显示什么浏览器

HTTP://本地主机:8080 /资源/ 2.png - >显示什么浏览器

HTTP://本地主机:8080 /资源/ js.js - >显示什么浏览器

HTTP://本地主机:8080 /资源/ c.css - >显示文件浏览器内容

HTTP://本地主机:8080 /资源/ test.html文件 - >显示文件浏览器内容

为什么不能我看到的图像,但可以看到的CSS和HTML内容? 我认为它是因为不正确的MIME类型设置?

PS:我想通过一个Servlet服务于静态文件和设置正确的MIME类型 - >一切完美的浏览器(Chrome),我可以看到的图像和.js文件的内容(和所有其他的文件结尾)。

我的servlet代码(应用程序正在运行,在“/”服务器的):

@WebFilter("/*")  //get all requests
public class MasterFilter implements javax.servlet.Filter {
        //...
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    HttpServletRequest req = (HttpServletRequest) request;
    String path = req.getRequestURI();

    if (path.startsWith("/resource")) {         
        String mimetype = "text/html;charset=UTF-8";

        mimetype = path.contains(".png") ? "image/png" : mimetype;
        mimetype = path.contains(".jpg") || path.contains(".jpeg") ? "image/jpeg" : mimetype;
        mimetype = path.contains(".js") ? "text/javascript" : mimetype;
        mimetype = path.contains(".css") ? "text/css" : mimetype;
        response.setContentType(mimetype);
        chain.doFilter(request, response); // Goes to static resource in local folder "webapp/resource/"

    }
}

有什么建议么 ? 提前致谢。

编辑:

与Servlet过滤器的上述解决方案正常工作。 但由于@ JGlass的答案,我还发现了另一种解决方案。 (记住,我绝对需要我的Servlet过滤器):

  1. “MasterFilter” 级转发给 “的serveResource” 的servlet:

    @WebFilter("/*") public class MasterFilter implements javax.servlet.Filter { /*...*/ public void doFilter(..){ /*...*/ if (path.startsWith("/resource")) {//forward to "ServeResource" servlet } } }

2. “的serveResource” 的servlet提供静态文件:

@WebServlet("/resource/*")
public class ServeResource extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public ServeResource() {
    super();
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 
   response)
 */
protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
throws ServletException, IOException {

  ServletContext cntx= req.getServletContext();
  String fileUri = req.getRequestURI();
  System.out.println("fileUri: "+fileUri);
  // Get the absolute path of the image (or any file)
  String filename = cntx.getRealPath(fileUri);
  System.out.println("file realPath: "+filename);
  // retrieve mimeType dynamically
  String mime = cntx.getMimeType(filename);
  System.out.println("mime type: "+mime);
  if (mime == null) {
    resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    return;
  }

  resp.setContentType(mime);
  File file = new File(filename);
  resp.setContentLength((int)file.length());

  FileInputStream in = new FileInputStream(file);
  OutputStream out = resp.getOutputStream();

  // Copy the contents of the file to the output stream
   byte[] buf = new byte[1024];
   int count = 0;
   while ((count = in.read(buf)) >= 0) {
     out.write(buf, 0, count);
  } 
    out.close();
    in.close();
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
}

}

3.web.xml包含扩展名映射:

    <mime-mapping>
    <extension>html</extension>
    <mime-type>text/html</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>txt</extension>
    <mime-type>text/plain</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>js</extension>
    <mime-type>text/javascript</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>jpg</extension>
    <mime-type>image/jpeg</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>jpeg</extension>
    <mime-type>image/jpeg</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>png</extension>
    <mime-type>image/png</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>css</extension>
    <mime-type>text/css</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>zip</extension>
    <mime-type>application/zip</mime-type>
</mime-mapping>

Answer 1:

你可以把你的MIME类型映射在web.xml。 下面是一个例子:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>TestDynamicWAR</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>testServlet</servlet-name>
        <servlet-class>com.mycompany.test.TestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>testServlet</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>

    <mime-mapping>
        <extension>html</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>txt</extension>
        <mime-type>text/plain</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>jpg</extension>
        <mime-type>image/jpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>png</extension>
        <mime-type>image/png</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>js</extension>
        <mime-type>text/plain</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>css</extension>
        <mime-type>text/css</mime-type>
    </mime-mapping> 

</web-app>

注意:您可能不需要的CSS和JS,他们已着手处理。 HTML和TXT MIME类型只是一个例子,以及

我不相信你需要处理的Servlet过滤器,如果你只是想处理MIME类型。 这个所谓后有服务器小程序与MIME类型处理输出从servlet的图像文件



文章来源: Wildfly Undertow File Mimetypes