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?
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:
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