Vitor Braga
Vitor Braga

Reputation: 2212

Showing image with absolute path in JSP

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:

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

Upvotes: 2

Views: 16700

Answers (2)

BalusC
BalusC

Reputation: 1108632

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>

Upvotes: 2

jamesmortensen
jamesmortensen

Reputation: 34038

Judging by the words you used in your URI, it looks like you are putting the path to the file on the filesystem in the src attribute instead of the URL that uniquely identifies the resource.

Absolute paths are not absolute from your filesystem, they are absolute from the root of your Web server. Additionally, those URL resource identifiers can even be changed or modified to map to different folders in your server or even dynamic resources.

Here is an example:

Path to file on server:

/home/website/images/image1.png

example.com is mapped to "website" folder:

When using a relative URL, perhaps you're seeing:

<img src="/images/image1.png" />

To use the absolute URL, you would need the HTTP scheme:

<img src="http://example.com/images/image1.png" />

In summary, using file paths to identify resources on your server from the client side HTML is just incorrect and will not work.

Upvotes: 0

Related Questions