tags:

views:

235

answers:

2

I want to display the image located in my local drive. I am using sun java app server 8. For example If I generate a file abc.jpg dynamically and store it in c:\abc.jpg, then how could I use it in jsp or servlets? How to display it in the jsp or servlet pages? I know giving the path c:\abc.jpg in coding to display image wont work , because it is out of webserver..

+2  A: 

Basically just create a Servlet which gets an InputStream of it with help of FileInputStream and writes it to the OutputStream of the HttpServletResponse along with a correct set of response headers with at least the content-type. Finally call this servlet in the src attribute of the <img> element along with the file identifier as request parameter or pathinfo. E.g.:

File file = new File("c:/abc.jpg");

response.setContentType(getServletContext().getMimeType(file.getName()));
response.setContentLength(file.length());
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

BufferedInputStream input = null;
BufferedOutputStream output = null;

try {
    input = new BufferedInputStream(new FileInputStream(file));
    output = new BufferedOutputStream(response.getOutputStream());

    byte[] buffer = new byte[10240];
    int length;
    while ((length = input.read(buffer)) > 0) {
        output.write(buffer, 0, length);
    }
} finally {
    if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
    if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
}

You can find here a complete basic example: http://balusc.blogspot.com/2007/04/imageservlet.html

BalusC
Thanks a lot. Still I have one problem..If I want to use it in my jsp page , How could I use? will it be possible to use it along with my jsp codings?
Senthil
You really don't want to use JSP for this. The image accounts as a **separate** http request and it returns **binary** data, not text data. I also don't understand the aversion most starters have against servlets. It's just another Java class. Is it the registration in web.xml which scares you or you don't understand? Then just say that so instead of asking how to achieve the same the wrong ways.
BalusC
I understood. I got the output as I need.. I am an beginer ofcourse, So Please explain me the following codingsresponse.setContentType(getServletContext().getMimeType(file.getName()));response.setContentLength(file.length());response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
Senthil
The first sets the content type in response header, so that browser knows how to handle it (as text? as image? as pdf? etc). The second sets the content length so that browser know how long it may take to finish the request (so that the browser's progress bar is more realistic and so on). The third sets the content-disposition to inform browser how to handle the image (embed in page, or display "save as" popup) and which default filename it should use when the client want to save the image. This all is just required by the HTTP specification: http://www.w3.org/Protocols/rfc2616/rfc2616.html
BalusC
+2  A: 

Maybe everybody is misunderstanding the question.

If the images are on your local drive and you want your Web server to serve them up to the world, then as a first step you need to upload them to your Web server.

That done, you can use URLs in <img> tags to refer to their location on the server.

Carl Smotricz
The question is too ambiguous. For the same money we can say that the (development) server runs at the very same physical machine and that he actually means "webcontent" instead of "webserver".
BalusC