tomcat虚拟路径——webapps目录外部静态资源访问

项目中突然注意到:在获取项目中的图片时候,加入项目名称是photoWeb,部署到tomcat后,

<Context docBase="photoWeb" path="/photoWeb" reloadable="true"/>

context配置是上面那样。但是我发现访问图片的时候,路径是这样子的:http://localhost/photoWeb/upload/shop/1001/12345678.jpg

就去webContent里面找对应的路径,但是没有找到。这时才意识到可以配置tomcat目录webapps以外的目录作为项目的访问目录,特别是在图片服务器应用的时候。可以把上传的图片统一放在指定的路径下。只需要把docBase修改为指定路径,可以使用绝对路径。当时在网上查了许久,基本都是这样写的:

docBase="D:/upload/img" path="/photoWeb"

但是,我改成如此之后,tomcat大概300多毫秒就启动完毕,根本没有找到我的项目,不知道为什么,也不清楚是不是tomcat6的问题,没有测试tomcat7与8。

最终修改为:

<Context docBase="D:/upload/img" path="/photoWeb/upload/img" reloadable="true"/>

把path属性也加上了图片以后的路径,这样就既可以访问D:/upload/img路径下的静态资源也可以访问项目webcontent里面的资源了。具体原有不是太清楚。去了tomcat官网,查了一下docBase属性的解释:

1.The Document Base (also known as the Context Root) directory for this web application, or the pathname to the web application archive file (if this web application is being executed directly from the WAR file). You may specify an absolute pathname for this directory or WAR file, or a pathname that is relative to the appBase directory of the owning Host.

2.The value of this field must not be set unless the Context element is defined in server.xml or the docBase is not located under the Host's appBase.

3.If a symbolic link is used for docBase then changes to the symbolic link will only be effective after a Tomcat restart or by undeploying and redeploying the context. A context reload is not sufficient.

1.是对docBase属性的值的解释,该值代表web应用的根路径,或者该web应用是war包的话,就是其归档路径(archivede 的含义:我的理解是war包里面的根目录吧);
2.除非Context元素被定义在server.xml中或者docBase不位于Host元素属性appBase的目录下,否则docBase禁止设值。
3.讲的是软连接,和这个应该没有关联。symbolic link 指的是软链接。

大概就是这样了,记录一下。如果哪位大神指导为什么的话,请多多赐教!谢谢!

猜你喜欢

转载自blog.csdn.net/ls0111/article/details/78542279
今日推荐