質問

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