Serving a resource from outside the context root in Spring MVC

Posted on 19-01-2013 12:16 by graham
This tutorial will explain how you should configure your Spring MVC application if you want to serve resources that reside outside the webapp directory, or even outside the whole project archive. In short, how to access files from anywhere in the file system with your static resources.

The standard case


Usually you keep all of your resources in a folder called resources that resides directly in the webapp folder. Here is the directory structure I am talking about:
--- src
--- main
--- webapp
--- resources
--- WEB-INF

In this case you configure access to your static resource by putting the following line in the servlet-context.xml file:
<mvc:resources mapping="/resources/**" location="/resources/" />

Serving resources from outside the context root


Now, this is accomplished by specifying an absolute path to the resource folder in the location attribute of the resources tag:
<mvc:resources mapping="/images/**" location="file:/absolute/path/to/the/resource/folder/" />

NOTE: don't forget to put the trailing slash at the end of the absolute path. It won't work if you miss that.

Serving resources from outside the context root - Windows path


Doing the same for Windows - you might wonder what the exact format of the location attribute should be, so here it is:
<mvc:resources mapping="/images/**" location="file:///c:/absolute/path/to/the/resource/folder/" />

Hope this will spare you some sleepless hours, because it took me a while to figure it out myself.
Comments
Thanks for your post it's exactly what I'm looking for. But unfortunately it does not work for me...My app-servlet.xml:
<mvc:annotation-driven />
<mvc:resources mapping="/pics/**" location="file:///c:/Tomcat6/webapps/resources/" />

My JSP-Call:
<img src="<c:url value="/pics/test.gif"/>" />

Do you know, what's going wrong?
Added on 18-03-2013 10:19 by anonymous
What is the exact value that <c:url="<c:url value="/pics/test.gif"/>"> renders? Is it "/pics/test.gif" or anything else? The whole result URL should be yourdomain.com/pics/test.gif.

Also, are you using Spring Security? If yes, make sure access privileges do not block this URL. To make it available to all users, set
<security:intercept-url pattern="/pics/.*" access="permitAll" />
Added on 18-03-2013 16:28 by graham
Hi graham, thanks for your help!
It renders to /AppName/pics/test.gif. The /AppName folder is under the "Tomcat_6\webapps" folder. I also added your access privileges to the securityContext.xml, but same behavior as before.
Added on 19-03-2013 07:50 by anonymous
Hi! Can you please tell me what is the context under which the app is deployed? Is it "AppName" or empty?

The "pics" folder should be available at http://yourdomain/<context>/pics. But context can be empty, in which case the address will be http://yourdomain/pics.

One more thing that might give us a clue - are you deploying this locally on localhost or on a remote server through some domain name?
Added on 19-03-2013 15:45 by graham
After a few hours spending on this topic, I found a solution.
Just changed the <servlet-mapping> url pattern from *.html to / in the web.xml configuration. Now everything is working. Thanks for your help!
Added on 20-03-2013 08:48 by anonymous
Hi,

I would like to know how I can access a resource in a remote machine using Spring. I will be hitting the server with the file name as a parameter. All the files will be in some remote machine. How can I access such resources?
Added on 10-07-2013 12:19 by anonymous
Hi,

I would like to know how I can access a resource in a remote machine using Spring. I will be hitting the server with the file name as a parameter. All the files will be in some remote machine. How can I access such resources?
Added on 10-07-2013 12:19 by anonymous
Thank you for this simple solution. This has helped me a lot, I was finding it really difficult to find a helpful answer for this problem.
Added on 27-06-2015 00:58 by anonymous

 

Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
Email:
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems