views:

66

answers:

2

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 access it in another page and when I try to access it from my JSP, it will not show up, I am guessing my path to the photo is incorrect

The code in my JSP to access the photo looks like the following.

            <tr>
                <td>
                    <img src="/usr/local/agent/photo-name-here.jpg"/>
                </td>
            </tr>

Am I incorrect with this path to the photo? If it helps, I am running my web application from Tomcat which is in the directory

C:\Tomcat6

I will eventually be moving this over to a linux machine and expect to share the same path to the photo.

+1  A: 

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.

Garis Suero
You can also use Apache's `Alias` directive (if you are using Apache) to make any particular folder on your filesystem accessible via any particular URL path. (If you're not using Apache, whatever server you are using probably has some equivalent to this directive.)
David Zaslavsky
Does this post apply even if I am running my web application in /var/lib/tomcat6/webapps/myapp ? Am I supposed to upload these files local to myapp?
CitadelCSAlum
You will need to upload your images to `/var/lib/tomcat6/webapps/myapp/images/` for example. and access it through the url : `http://mydomain/images/yourimage.jpg`
Garis Suero
Wouldnt this be a bad practice to store the images alongside the web application if the images were to be uploaded after the deployment of the application? In the event that I needed to re deploy the application, I would lose all images that were uploaded following the last deployment.
CitadelCSAlum
+2  A: 

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

  1. 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"/>
    
  2. 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"/>
    
BalusC
I like these ideas...the first one seems a little more fit for what I am doing. Thank you. Are there any downsides to doing #1? My other option is I have a folder near my jsp's /jsp/assets/images/ where It is possible I can make a directory, but there is a possibility that this folder would hold a hundred, maybe more photos. I imagine this would be a bad practice to store in that directory. Am I right in assuming this?
CitadelCSAlum
I don't forsee any downsides to #1. It's the easiest and best way. Another way is indeed to store them directly in the public webcontent, but you have to take into account that they will all get lost whenever you redeploy the WAR and those files are not included in the new WAR.
BalusC
@BalusC I did exactly what you did under #1 and put that line in my server.xml file. The photo is still not showing up. Is there something else I need to configure?
CitadelCSAlum
How are you managing Tomcat? From commandline or using an IDE like Eclipse? When using Eclipse, Tomcat's own `server.xml` will be by default ignored.
BalusC
I am using Spring MVC and using eclipse as an IDE but I am building and exporting the app to my tomcat/webapps folder
CitadelCSAlum
Wait, in your question you're using two different paths, `/usr/local/rac` and `/usr/local/agent`. The `docBase` attribute of the `<Context>` must point to the real local disk file system path and the `path` must specify the context root name as you use as "subfolder" in URL. The `<Context>` element itself should go inside the `<Host>` element. Did you set it all right? I'd also read the startup and error logs in `/logs` folder for any info and problems. More detail about `<Context>`: http://tomcat.apache.org/tomcat-6.0-doc/config/context.html
BalusC
@BalusC, thanks for all the help, I looked over the documentation, looked into my log files and still am unable to figure out what the problem is. In my <pre>server.xml</pre> , I have defined the following Context docBase="/usr/local/mmz/agent/images" path="images" the docBase is the exact path to the directory where I am storing images. The markup in my jsp to access the images is img src="/images/picture-name-here.jpg" but I am still unable to see the picture.
CitadelCSAlum
The `path` has to start with a slash: `path="/images"`, exactly as in my answer. Otherwise it will return a `HTTP 400 Bad Request` (you could determine that with under each Firebug).
BalusC
@BalusC, that was a typo, it is in fact path="/images" in my code, I just made an inccorect path in my comment above.
CitadelCSAlum
It should work. Don't you have multiple Tomcat instances and are you editing and/or starting the wrong one? Do the docroot folder provide sufficient read/write rights? By the way, after every edit you should restart the server to have the changes to make effect.
BalusC
To be sure, I just did a test on both Tomcat 6.0.29 and 7.0.2 and it works fine. I'm however running Windows XP. Isn't the `<img src>` in your JSP plain wrong? Try testing it by directly entering the full URL in browser address bar.
BalusC
What do you mean by plain wrong, I believe my jsp is correct?
CitadelCSAlum
ok BalusC, i believe its making progress, when i typed in myipaddress/images/ring.jpg where ring.jpg is the image, it went to a page and gave me a broken image icon, so it must be working, but the image is not showing up? I have checked permissions and verified the image is in that directory. I even put a test.html file in there and hit the same URL and it works, but the image wont show up
CitadelCSAlum
It's case sensitive. Maybe a case error? Or maybe the image itself is broken? At least, it *works* :)
BalusC
yeah it does work, i checked firebug, and it says that it is giving me a 404 error, which means it must be looking for that. It says "failed to load resource, server responded with a status of 404" but Im not sure why...i guess ill have to look into it, thank you so much for the help getting this far
CitadelCSAlum
Try a different image file. Try renaming the image file name.
BalusC
hey its working!, i dont know what it is, but it didnt like that file, i believe it may have been a corrupted photo, I cant thank you enough for your help!
CitadelCSAlum
Cheer! You're welcome. But why was the accepted mark removed?
BalusC
on accident sorry about that, It seems I am having some trouble with my uploads, and I have seen from your blog you are well experienced on uploads, I am going to post another question regarding my problem, feel free to take a gander, otherwise, i know you have spent enough time and I appreciate your help!
CitadelCSAlum
I've seen your other question about that and you seem to be using Spring. This is beyond my knowledge. But just post it and I'll see if I can reasonably answer it based on the symptoms, else just someone else will do :)
BalusC