Showing image with absolute path in JSP

15,970

Indeed, the <img src> must refer a public web URL, not a local disk file system path. Your images have to be available by a HTTP request. It's namely the webbrowser who has got to download them, not the webserver who has got to include them somehow.

The most straightforward way for starters would be to create a servlet which gets the image content as an InputStream by FileInputStream based on the request parameter or path info and then writes it the usual Java IO way to OutputStream of the HttpServletResponse, after having set the necessary HTTP response headers so that the browser understands how to deal with it.

E.g., assuming that the servlet is mapped on /images/* and you open the image as http://example.com/contextpath/images/image1.png:

String filename = request.getPathInfo();
File file = new File("/home/website/images", filename);

response.setHeader("Content-Type", getServletContext().getMimeType(filename));
response.setHeader("Content-Length", String.valueOf(file.length()));

InputStream input = new FileInputStream(file);
OutputStream output = response.getOutputStream();
// Stream bytes the usual way.

An alternative is to add /home/website/images as a new web application context to the server with the context path of /images. This way the image would be available by http://example.com/images/image1.png You're only dependent on the servletcontainer make/version how to configure that and also whether you've full admin control over it. For Jetty, that would be the following if you're managing it programmatically:

server.setHandler(new WebAppContext("/home/webapp/images", "/images"));

Or when you're managing it by XML:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="resourceBase">/home/website/images</Set>
    <Set name="contextPath">/images</Set>
</Configure>
Share:
15,970
Vitor Braga
Author by

Vitor Braga

I'm someone who loves computing and everything that's technology. I love specially WEB and Mobile, and I would like to work with that.

Updated on June 04, 2022

Comments

  • Vitor Braga
    Vitor Braga almost 2 years

    I'm using Java, JSP and Jetty to develop a website.

    I'm having some problem with images paths passed in src of <img> tags.

    In my database, I have the Linux absolute path of the users' images. I get this value and put into the "src" attribute.

    I have the path, for example:

    • /home/website/images/image1.png

    Making this way, the image doesn't appear. Thinking for a long time, I've concluded that's natural because I've never seen in any website image with such path to an absolute path of the OS.

    What I need to know is what I have to do?

    Do I have to alter Jetty's configuration? Do I have to run some code in Java?

    Searching through Google, I got more confused. I would like an explanation.


    UPDATE (26/03/2013)

    I found this way of treating this issue:

    http://balusc.blogspot.com.br/2007/04/imageservlet.html

    Thank you BalusC! hauhauhahaua

  • Vitor Braga
    Vitor Braga almost 12 years
    Excellent! About the first solution, wouldn't it be some kind of not efficient, supposing I would have too many images? About the 2º solution, which I liked more, where I would set this new WebAppContext? Could I do this in a xml file too? Thanks very much!! =]
  • Vitor Braga
    Vitor Braga almost 12 years
    I've put the same the code in context.xml same as you mentioned, but it didn't work...are you sure?
  • BalusC
    BalusC almost 12 years
    Sorry, the <Set name="webApp"> has to be <Set name="resourceBase">. As to efficiency of 1st solution, it isn't worse as to streaming, but it doesn't respect browser cache requests.
  • Vitor Braga
    Vitor Braga almost 12 years
    Thanks for the update! I've updated my context.xml file and I still don't get the images. (It would be supposed to get the image by example.com/im/image1.png, but it still doesn't see the /im folder and the image does not appear... I'm desperate...
  • BalusC
    BalusC almost 12 years
    Sorry, I don't use Jetty. I'm just answering based on its documentation.