1

i'm trying to find a way remap a path to an absolute one in order to retrieve images stored in the server filesystem, i'm also using spring mvc but the <mvc:resource> can't map absolute paths, and i can't find a nice way to do this with spring controllers

i.e. I need to map all the image requests from /images/** to /root/var/img/**, in this way when a client browser try to load an image of the page, every images stays in the path above mentioned

Gnappuraz
  • 250
  • 3
  • 12

2 Answers2

2

You can create a new context with docBase referencing to your folder (/root/var/img) It should look like this:

<Context path=”/images” docBase=”/root/var/img/” ... >
</Context>

Refer to Tomcat context configuration documentation for more details (e.g., for Tomcat 6: Apache Tomcat Configuration Reference: The Context Container).

Introduction to this document lists possible places where context elements can be defined.

EDIT

As I mentioned in comments, Spring-specific way to do this without creating any new context seems to be using RedirectView (link to Spring v2.5 JavaDoc). According to JavaDoc:

"View that redirects to an absolute, context relative, or current request relative URL, by default exposing all model attributes as HTTP query parameters."

EDIT2

It seems I've misunderstood RedirectView purpose, which is a good old URL redirection. Thus, answer by @Jens should be more appropriate (+1). I still wonder if there's an existing standard solution in Spring (what I originally thought RedirectView would do..) It's not like there's much code to write, but still :)

EDIT3 After reading more on this topic, I found out that <mvc:resources> is able to do this (i.e., mapping resource using not only relative path, or classpath), you just need to correctly configure location using file: prefix . In fact, there's already answer on SO that explains exactly what you need to do: Spring : serving static resources outside context root

Community
  • 1
  • 1
Art Licis
  • 3,619
  • 1
  • 29
  • 49
  • Thanks for the tip, but if possible i would prefer to not create a new context, a servlet based solution would be better for me... – Gnappuraz Aug 14 '11 at 14:23
  • @Gnappuraz Ahh, I see. What about Spring's [RedirectView](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/view/RedirectView.html) (link to v2.5 JavaDoc)? JavaDoc states: "View that redirects to an absolute, context relative, or current request relative URL, by default exposing all model attributes as HTTP query parameters." – Art Licis Aug 14 '11 at 14:28
  • the only standard solution i found is the tag, that allow this remap but only for relatives paths. – Gnappuraz Aug 15 '11 at 07:42
  • @Gnappuraz: I've read more on , and updated my answer. – Art Licis Aug 15 '11 at 08:55
1

not sure if I am getting that right but I think the common way to do that is to set up a controller which is mapped on the requests to /images/:

@RequestMapping(value ="/images/")    
public void fetchImage(@RequestParam String id, HttpServletResponse response)

Then the controller can load the requested image from a configured directory like "/root/var/img" and write it the the OutputStream of the response. E.g:

org.apache.commons.io.IOUtils.copy(fileInputStram, response.getOutputStream())

In addition you have to set the correct mime type etc. in the response.

Jens

Jens
  • 199
  • 1
  • 3
  • yes, i'm just trying to do something similar, with this controller @RequestMapping(value="/images/") public void fetchImage(@RequestParam String id, OutputStream output) – Gnappuraz Aug 15 '11 at 07:47