0

I am trying to display an image stored at local file system outside my webapp. following question: Simplest way to serve static data from outside the application server in a Java web application

EDIT: I want file to be outside the webapp cause these images are uploaded by user, If I put them inside webapp, I might loose them when I redeploy the web app

but the file is not being displayed on the webpage. When I try opening the file through: localhost:8080/images/imageName.jpg it gives me a resource not available error.

I have added the context in my server.xml (traversing throug Servers->Config->server.xml) :

 ........
 <Context docBase="DMSystemV1.0" path="/DMSystemV1.0" reloadable="true" source="org.eclipse.jst.jee.server:DMSystemV1.0"/>
      <Context docBase="/Projects/SpringExample/Images" path="/images"/>
      </Host>

Also my web.xml looks like this:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/static/*</url-pattern>
   </servlet-mapping>
   <servlet>
    <servlet-name>DMSystem</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>DMSystem</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

The place where I want it to get displayed: <img src="/images/${imagePath}" alt="Item's image">

Mytomcat is in: F:\Software\Servers\tomcat7\tomcat7 While the image folder is: F:\Projects\SpringExample\Images

Is the image path is taken relative to the tomcat folder?

Also,write now I am hard-coding the upload path (in my upload servlet) and download path in server.xml, is there a way to provide them as confign or set up info?

Community
  • 1
  • 1
Sudh
  • 1,265
  • 2
  • 19
  • 30
  • *"I am trying to display an image stored at local file system outside my webapp."* Why not include the image(s) in the web app.? – Andrew Thompson Dec 14 '12 at 04:41
  • While there are ways around it, web apps specifically do not / should not normally have access to the local file system. You'd have to write something that intercepts the request, loads the image from the file system, and returns it. – GreyBeardedGeek Dec 14 '12 at 04:46
  • @AndrewThompson: cause these images are uploaded by user, they might get deleted if I redeploy the web app – Sudh Dec 14 '12 at 06:10
  • @GreyBeardedGeek: yes...that's the reason I added new context to tomcat's server.xml, tomcat's default servlet should intercept the request and retrun the image? plz correct me if I am wrong – Sudh Dec 14 '12 at 06:12
  • Yes, you are wrong. You need to actually deploy an application (a .war file, or an 'exploded' directory structure that looks like one, complete with a WEB-INF/web.xml file) with that context. – GreyBeardedGeek Dec 14 '12 at 15:18
  • @GreyBeardedGeek: I don't understand, I have deployed the war file and it's working fine..except it's not displaying the images so my question is how do I make it display the images(which will be stored outside the webapp at a fixed location...) – Sudh Dec 14 '12 at 16:58
  • Best bet is to go with the proxy servlet answer that gerrytan gave you. If you just want to serve static files directly, without the proxy, that's not really supported. You could set up an Apache web server in front of Tomcat to do that, though. – GreyBeardedGeek Dec 14 '12 at 20:16

1 Answers1

2

Simplest solution would be to copy the image and serve it on your web-app root. However if this solution doesn't suit you for whatever reason, you can always create a servlet to 'proxy' your image:

Create a servlet, map it to a path, for example:

<servlet-mapping>
  <servlet-name>imageProxyServlet</servlet-name>
  <url-pattern>/imagesproxy/*</url-pattern>
</servlet-mapping>

Then on your servlet class, do a file IO to read your picture, and then write it to the response. Don't forget to set appropriate content type, and use buffered read/write to avoid blowing your memory if the image is large.

Then you can serve your image as http://mydomain.com/myapp/imagesproxy/something.jpg. Request to that URL will be dispatched into the images proxy servlet

Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
gerrytan
  • 40,313
  • 9
  • 84
  • 99
  • Tips: 1) For prettier formatting of XML in questions not tagged XML, use `` before the snippet. 2) Are you *sure* that snippet is well formed? Seems to need a closing ``. – Andrew Thompson Dec 14 '12 at 06:24