I have an app which should save content (user avatars) in some directory, for example C:\avadir. In my app I'm using SpringMVC. I need to show user defined avatars. For this I have to configure Tomcat to use this external directory. I have such opthions in my ROOT.xml, placed in %CATALINA_HOME%\conf\Catalina\localhost:

<Context path="/ava" docBase="c:/avadir" debug="0" reloadable="true" crossContext="true" />

and next settings in my servlet-context.xml:

<resources mapping="/ava/**" location="/ava/" />

After set up this settings I still can't get access to my file placed in C:\avadir\file.jpg by url localhost:8080/ava/file.jpg. Is there something missed?

有帮助吗?

解决方案

You can achieve what you want without modifying your ROOT.xml file which should make your application slightly more easy to manage.

So firstly I would remove the Context definition from ROOT.xml.

Secondly I would modify your current Spring MVC configuration to serve the images as part of the Spring MVC application. Using the path that you have suggested I would update your <resources> definition to:

<mvc:resources mapping="/ava/**" location="file:///C:/avadir"/>

This is essentially configuring your Spring MVC implementation to serve resources directly from the filesystem rather than relying upon the second context that you have configured in Tomcat.

You also need to remember that the Spring MVC resources mapping will be relative to the context of your web application. For example: if your application is deployed at http://example.com:8080/myApp then the /ava mapping will actually match when accessed with URL http://example.com:8080/myApp/ava/file.jpeg

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top