如何配置Tomcat从webapps外部的外部文件夹提供图像?

时间:2022-02-23 00:35:08

How can i use Tomcat to serve image files from a public folder outside webapps? I dont want to use a 2nd Apache fileserver on a different port since the image files are part of the same app. And i dont want to create a symlink to the public folder inside webapps since my app is deployed as a war file....Is there a simpler solution similar to using default servlet for static content inside webapps, for static content outside outside webapps

如何使用Tomcat从webapps外部的公用文件夹提供图像文件?我不想在不同的端口上使用第二个Apache文件服务器,因为图像文件是同一个应用程序的一部分。我不想为webapps中的公共文件夹创建一个符号链接,因为我的应用程序被部署为war文件....有一个更简单的解决方案类似于使用默认servlet来处理webapps中的静态内容,对于外部webapps之外的静态内容

8 个解决方案

#1


You could have a redirect servlet. In you web.xml you'd have:

你可以有一个重定向servlet。在你的web.xml中你有:

<servlet>
    <servlet-name>images</servlet-name>
    <servlet-class>com.example.images.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>images</servlet-name>
    <url-pattern>/images/*</url-pattern>
</servlet-mapping>

All your images would be in "/images", which would be intercepted by the servlet. It would then read in the relevant file in whatever folder and serve it right back out. For example, say you have a gif in your images folder, c:\Server_Images\smilie.gif. In the web page would be <img src="http:/example.com/app/images/smilie.gif".... In the servlet, HttpServletRequest.getPathInfo() would yield "/smilie.gif". Which the servlet would find in the folder.

您的所有图像都将位于“/ images”中,这些图像将被servlet拦截。然后它将在相关文件中读取任何文件夹并立即将其提供给它。例如,假设您的images文件夹中有一个gif,c:\ Server_Images \ smilie.gif。在网页中将是如何配置Tomcat从webapps外部的外部文件夹提供图像?

#2


This is the way I did it and it worked fine for me. (done on Tomcat 7.x)

这就是我做的方式,它对我来说很好。 (在Tomcat 7.x上完成)

Add a <context> to the tomcat/conf/server.xml file.

添加到tomcat / conf / server.xml文件中。

Windows example:

<Context docBase="c:\Documents and Settings\The User\images" path="/project/images" />

Linux example:

<Context docBase="/var/project/images" path="/project/images" />

Like this (in context):

像这样(在上下文中):

<Server port="8025" shutdown="SHUTDOWN">
 ...
  <Service name="Catalina">
    ...
    <Engine defaultHost="localhost" name="Catalina">
     ...
     <Host appBase="webapps"
      autoDeploy="false" name="localhost" unpackWARs="true"
      xmlNamespaceAware="false" xmlValidation="false">
      ...
      <!--MAGIC LINE GOES HERE-->
       <Context  docBase="/var/project/images" path="/project/images" />

      </Host>
    </Engine>
  </Service>
</Server>

In this way, you should be able to find the files (e.g. /var/project/images/NameOfImage.jpg) under:

通过这种方式,您应该能够在以下位置找到文件(例如/var/project/images/NameOfImage.jpg):

http://localhost:8080/project/images/NameOfImage.jpg 

#3


In Tomcat 7, you can use "aliases" property. From the docs:

在Tomcat 7中,您可以使用“别名”属性。来自文档:

This attribute provides a list of external locations from which to load resources for this context. The list of aliases should be of the form "/aliasPath1=docBase1,/aliasPath2=docBase2" where aliasPathN must include a leading '/' and docBaseN must be an absolute path to either a .war file or a directory. A resource will be searched for in the first docBaseN for which aliasPathN is a leading path segment of the resource. If there is no such alias, then the resource will be searched in the usual way. These external locations will not be emptied if the context is un-deployed.

#4


You can deploy an images folder as a separate webapp and define the location of that folder to be anywhere in the file system.

您可以将图像文件夹部署为单独的Web应用程序,并将该文件夹的位置定义为文件系统中的任何位置。

Create a Context element in an XML file in the directory $CATALINA_HOME/conf/[enginename]/[hostname]/ where enginename might be 'Catalina' and hostname might be 'localhost'.

在目录$ CATALINA_HOME / conf / [enginename] / [hostname] /中的XML文件中创建Context元素,其中enginename可能是'Catalina',主机名可能是'localhost'。

Name the file based on the path URL you want the images to be viewed from, so if your webapp has path 'blog', you might name the XML file blog#images.xml and so that your images would be visible at example.com/blog/images/

根据您希望从中查看图像的路径URL命名文件,因此,如果您的webapp具有路径“blog”,则可以将XML文件命名为blog#images.xml,以便在example.com上显示您的图像。 /博客/图片/

The content of the XML file should be <Context docBase="/filesystem/path/to/images"/>

XML文件的内容应该是

Be careful not to undeploy this webapp, as that could delete all your images!

小心不要取消部署此webapp,因为这可能会删除所有图像!

#5


Instead of configuring Tomcat to redirect requests, use Apache as a frontend with the Apache Tomcat connector so that Apache is only serving static content, while asking tomcat for dynamic content.

不要将Tomcat配置为重定向请求,而是使用Apache作为Apache Tomcat连接器的前端,以便Apache仅提供静态内容,同时向tomcat请求动态内容。

Using the JKmount directive (or others) you could specify exactly which requests are sent to Tomcat.

使用JKmount指令(或其他),您可以准确指定将哪些请求发送到Tomcat。

Requests for static content, such as images, would be served directly by Apache, using a standard virtual host configuration, while other requests, defined in the JKMount directive will be sent to Tomcat workers.

Apache使用标准虚拟主机配置直接提供静态内容(如映像)请求,而JKMount指令中定义的其他请求将发送给Tomcat worker。

I think this implementation would give you the most flexibility and control on the overall application.

我认为这种实现方式可以为您提供最大的灵活性和对整个应用程序的控制。

#6


After none of solutions based on defining XMLs worked for me, I found this answer very helpful. Took about a minute, and a small code change: I modified this line

在没有基于定义XML的解决方案对我有用之后,我发现这个答案非常有用。大约一分钟,一个小代码改变:我修改了这一行

this.basePath = getServletContext().getRealPath(getInitParameter("basePath"));

this.basePath = getServletContext()。getRealPath(getInitParameter(“basePath”));

into

this.basePath = getInitParameter("basePath");

this.basePath = getInitParameter(“basePath”);

#7


This is very simple and straight forward to server the static content from outside webapps folder in tomcat.

这非常简单直接地从tomcat中的webapps文件夹外部服务静态内容。

Simply edit the server.xml under $CATALINA_HOME/config/server.xml as below and restart the tomcat.

只需编辑$ CATALINA_HOME / config / server.xml下的server.xml,如下所示,然后重新启动tomcat。

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        **<Context docBase="C:\Ankur\testFiles"  path="/companyLogo" />**

       <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

Add the context element inside the host element with two attribute docBase and path.

使用两个属性docBase和path在host元素中添加context元素。

1) docBase: represents the hard drive directory 2) path: represents the uri on which you want to serve the static content.

1)docBase:表示硬盘目录2)path:表示要为其提供静态内容的uri。

For example:If you have 7.png inside the C:\Ankur\testFiles directory then you can access the 7.png file like below:

例如:如果您在C:\ Ankur \ testFiles目录中有7.png,那么您可以访问7.png文件,如下所示:

http://localhost:8081/companyLogo/7.png

For more details, check the blog

有关更多详细信息,请查看博客

#8


Many years later, we can do the following with Spring Web MVC, inside our webapp-servlet.xml file:

多年以后,我们可以在我们的webapp-servlet.xml文件中使用Spring Web MVC执行以下操作:

<mvc:resources mapping="/static/**" location="/static/" />

#1


You could have a redirect servlet. In you web.xml you'd have:

你可以有一个重定向servlet。在你的web.xml中你有:

<servlet>
    <servlet-name>images</servlet-name>
    <servlet-class>com.example.images.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>images</servlet-name>
    <url-pattern>/images/*</url-pattern>
</servlet-mapping>

All your images would be in "/images", which would be intercepted by the servlet. It would then read in the relevant file in whatever folder and serve it right back out. For example, say you have a gif in your images folder, c:\Server_Images\smilie.gif. In the web page would be <img src="http:/example.com/app/images/smilie.gif".... In the servlet, HttpServletRequest.getPathInfo() would yield "/smilie.gif". Which the servlet would find in the folder.

您的所有图像都将位于“/ images”中,这些图像将被servlet拦截。然后它将在相关文件中读取任何文件夹并立即将其提供给它。例如,假设您的images文件夹中有一个gif,c:\ Server_Images \ smilie.gif。在网页中将是如何配置Tomcat从webapps外部的外部文件夹提供图像?

#2


This is the way I did it and it worked fine for me. (done on Tomcat 7.x)

这就是我做的方式,它对我来说很好。 (在Tomcat 7.x上完成)

Add a <context> to the tomcat/conf/server.xml file.

添加到tomcat / conf / server.xml文件中。

Windows example:

<Context docBase="c:\Documents and Settings\The User\images" path="/project/images" />

Linux example:

<Context docBase="/var/project/images" path="/project/images" />

Like this (in context):

像这样(在上下文中):

<Server port="8025" shutdown="SHUTDOWN">
 ...
  <Service name="Catalina">
    ...
    <Engine defaultHost="localhost" name="Catalina">
     ...
     <Host appBase="webapps"
      autoDeploy="false" name="localhost" unpackWARs="true"
      xmlNamespaceAware="false" xmlValidation="false">
      ...
      <!--MAGIC LINE GOES HERE-->
       <Context  docBase="/var/project/images" path="/project/images" />

      </Host>
    </Engine>
  </Service>
</Server>

In this way, you should be able to find the files (e.g. /var/project/images/NameOfImage.jpg) under:

通过这种方式,您应该能够在以下位置找到文件(例如/var/project/images/NameOfImage.jpg):

http://localhost:8080/project/images/NameOfImage.jpg 

#3


In Tomcat 7, you can use "aliases" property. From the docs:

在Tomcat 7中,您可以使用“别名”属性。来自文档:

This attribute provides a list of external locations from which to load resources for this context. The list of aliases should be of the form "/aliasPath1=docBase1,/aliasPath2=docBase2" where aliasPathN must include a leading '/' and docBaseN must be an absolute path to either a .war file or a directory. A resource will be searched for in the first docBaseN for which aliasPathN is a leading path segment of the resource. If there is no such alias, then the resource will be searched in the usual way. These external locations will not be emptied if the context is un-deployed.

#4


You can deploy an images folder as a separate webapp and define the location of that folder to be anywhere in the file system.

您可以将图像文件夹部署为单独的Web应用程序,并将该文件夹的位置定义为文件系统中的任何位置。

Create a Context element in an XML file in the directory $CATALINA_HOME/conf/[enginename]/[hostname]/ where enginename might be 'Catalina' and hostname might be 'localhost'.

在目录$ CATALINA_HOME / conf / [enginename] / [hostname] /中的XML文件中创建Context元素,其中enginename可能是'Catalina',主机名可能是'localhost'。

Name the file based on the path URL you want the images to be viewed from, so if your webapp has path 'blog', you might name the XML file blog#images.xml and so that your images would be visible at example.com/blog/images/

根据您希望从中查看图像的路径URL命名文件,因此,如果您的webapp具有路径“blog”,则可以将XML文件命名为blog#images.xml,以便在example.com上显示您的图像。 /博客/图片/

The content of the XML file should be <Context docBase="/filesystem/path/to/images"/>

XML文件的内容应该是

Be careful not to undeploy this webapp, as that could delete all your images!

小心不要取消部署此webapp,因为这可能会删除所有图像!

#5


Instead of configuring Tomcat to redirect requests, use Apache as a frontend with the Apache Tomcat connector so that Apache is only serving static content, while asking tomcat for dynamic content.

不要将Tomcat配置为重定向请求,而是使用Apache作为Apache Tomcat连接器的前端,以便Apache仅提供静态内容,同时向tomcat请求动态内容。

Using the JKmount directive (or others) you could specify exactly which requests are sent to Tomcat.

使用JKmount指令(或其他),您可以准确指定将哪些请求发送到Tomcat。

Requests for static content, such as images, would be served directly by Apache, using a standard virtual host configuration, while other requests, defined in the JKMount directive will be sent to Tomcat workers.

Apache使用标准虚拟主机配置直接提供静态内容(如映像)请求,而JKMount指令中定义的其他请求将发送给Tomcat worker。

I think this implementation would give you the most flexibility and control on the overall application.

我认为这种实现方式可以为您提供最大的灵活性和对整个应用程序的控制。

#6


After none of solutions based on defining XMLs worked for me, I found this answer very helpful. Took about a minute, and a small code change: I modified this line

在没有基于定义XML的解决方案对我有用之后,我发现这个答案非常有用。大约一分钟,一个小代码改变:我修改了这一行

this.basePath = getServletContext().getRealPath(getInitParameter("basePath"));

this.basePath = getServletContext()。getRealPath(getInitParameter(“basePath”));

into

this.basePath = getInitParameter("basePath");

this.basePath = getInitParameter(“basePath”);

#7


This is very simple and straight forward to server the static content from outside webapps folder in tomcat.

这非常简单直接地从tomcat中的webapps文件夹外部服务静态内容。

Simply edit the server.xml under $CATALINA_HOME/config/server.xml as below and restart the tomcat.

只需编辑$ CATALINA_HOME / config / server.xml下的server.xml,如下所示,然后重新启动tomcat。

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        **<Context docBase="C:\Ankur\testFiles"  path="/companyLogo" />**

       <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

Add the context element inside the host element with two attribute docBase and path.

使用两个属性docBase和path在host元素中添加context元素。

1) docBase: represents the hard drive directory 2) path: represents the uri on which you want to serve the static content.

1)docBase:表示硬盘目录2)path:表示要为其提供静态内容的uri。

For example:If you have 7.png inside the C:\Ankur\testFiles directory then you can access the 7.png file like below:

例如:如果您在C:\ Ankur \ testFiles目录中有7.png,那么您可以访问7.png文件,如下所示:

http://localhost:8081/companyLogo/7.png

For more details, check the blog

有关更多详细信息,请查看博客

#8


Many years later, we can do the following with Spring Web MVC, inside our webapp-servlet.xml file:

多年以后,我们可以在我们的webapp-servlet.xml文件中使用Spring Web MVC执行以下操作:

<mvc:resources mapping="/static/**" location="/static/" />