In my web application one of my pages is uploading a photo to the path
/usr/local/rac/picture-name-goes-here
The photo is uploading fine, but I need to acces
There is one major misconception here. HTML is executed by the webbrowser, not by the webserver. The webbrowser downloads HTML, scans for any resources which needs to be downloaded as well (CSS, scripts, images, etc) and fires a new HTTP request for each of them. All resources should point to a valid URL, not to some local disk file system path which the client machine has no notion of.
There are basically two ways to solve this "problem":
Add a new Context
to Tomcat's /conf/server.xml
:
<Context docBase="/usr/local/agent" path="/images" />
This way they'll be accessible through http://example.com/images/... and you'll be able to use the following <img>
<img src="/images/photo-name-here.jpg"/>
Create a Servlet
which basically gets an InputStream
of the image and writes it to the OutputStream
of the response along a correct set of headers. You can find here a basic example of such a servlet and here a more advanced example. When the Servlet
is mapped on /images/*
in web.xml
, the images are accessible by http://example.com/contextname/images/... and you'll be able to use it as follows (assuming that the JSP/HTML file is located in the context root):
<img src="images/photo-name-here.jpg"/>
src="/usr/local/agent/photo-name-here.jpg"
<- this URL is a local address in your server, to show up your images you have to set a valid HTTP
address like:
http://www.yourdomain.com/images/photo-name-here.jpg
To accomplish that you will need to upload the foto to a localpath
that is inside in your www
root folder.
If your webapp
is installed in
/home/apache/www/website/
you will upload your images to a folder like:
/home/apache/www/website/images/
and then your HTTP
address will be
http://www.yourdomain.com/images/photo-name-here.jpg
I got a little confuse with your two paths in /usr/
and C:\Tomcat
I encourage you to put the upload localpath
folder parametrized, so you will be only modifying the config file instead of every function or method that access to that local path.