如何配置 Tomcat 以从 webapps 外部的外部文件夹提供图像? [英] How to config Tomcat to serve images from an external folder outside webapps?
问题描述
如何使用 Tomcat 从 webapps 外部的公共文件夹提供图像文件?我不想在不同的端口上使用第二个 Apache 文件服务器,因为图像文件是同一个应用程序的一部分.而且我不想创建指向 webapps 内公共文件夹的符号链接,因为我的应用程序被部署为一个 war 文件....../p>
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
推荐答案
你可以有一个重定向 servlet.在您的 web.xml 中,您将拥有:
You could have a redirect servlet. In you web.xml you'd have:
<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>
您的所有图像都将在/images"中,它会被 servlet 拦截.然后它会读入任何文件夹中的相关文件并立即将其返回.例如,假设您的图像文件夹中有一个 gif,c:\Server_Images\smilie.gif.在网页中将是 <img src="http:/example.com/app/images/smilie.gif"...
.在 servlet 中,HttpServletRequest.getPathInfo()
将产生/smilie.gif".servlet 会在文件夹中找到哪个.
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.
这篇关于如何配置 Tomcat 以从 webapps 外部的外部文件夹提供图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!