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 Answers
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.setHeader("Content-Length", String.valueOf(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

- 1,082,665
- 372
- 3,610
- 3,555
-
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? – professionalcoder2010 Jan 04 '10 at 17:22
-
3You 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 Jan 04 '10 at 17:27
-
I understood. I got the output as I need.. I am an beginer ofcourse, So Please explain me the following codings response.setContentType(getServletContext().getMimeType(file.getName())); response.setContentLength(file.length()); response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\""); – professionalcoder2010 Jan 04 '10 at 18:45
-
1The 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 Jan 04 '10 at 18:57
-
Why don 't you close the outputstream? For example: final ServletOutputStream outputStream = response.getOutputStream(); Files.copy(source, outputStream); outputStream.close(); I mean in the example in your blog page.. – Koray Tugay Oct 21 '17 at 11:08
-
@KorayTugay: Because I didn't create it myself: https://stackoverflow.com/q/1829784 – BalusC Oct 21 '17 at 11:21
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.

- 66,391
- 18
- 125
- 167
-
1The 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 Jan 04 '10 at 16:48
-
@Carl can we directly serve image on `jsp` from local Drive without upload that image to web server – HybrisHelp Jul 30 '13 at 10:22
-
1@ankit337: Yes, provided that you can be sure the file will be in a known and constant place on your local machine. You would use something like `
`. – Carl Smotricz Aug 01 '13 at 14:06