how to use the image file located in local drive, for printing it in jsp page or servlet page?


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..


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: <a href="http://balusc.blogspot.com/2007/04/imageservlet.html" rel="nofollow">http://balusc.blogspot.com/2007/04/imageservlet.html</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.


  • Retaining a column with all strings during groupby on a pandas dataframe
  • TableRow object not returning Record type columns in dataFlow Job
  • Redefinition different basic types => typedef
  • Convert com.lowagie.text.Image to BufferedImage
  • Converting tuples into rows from numerous columns in a pandas DataFrame
  • Firestore query documents startsWith a string
  • How often to derive from boost::signals::trackable?
  • The API package 'channel' or call 'CreateChannel()' was not found
  • File Upload handler
  • ASP.NET Capture and replace output in Global.asax
  • How to send image as base64 string in JSON using HTTP POST in Android?
  • ExtJS 4 Spring 3 file upload. Server sends bad response content type
  • How to silently drop a request in Tomcat?
  • How can I configure HTTP Response Headers in a Struts2 Interceptor?
  • Type mismatch: cannot convert from Connection to Connection
  • Where does the file get saved using “File file = new file(filename)” in Android
  • Extjs, handling success or failure when doing a standard submit in a form
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Javascript CORS - No 'Access-Control-Allow-Origin' header is present
  • Struts 2 TextField Tag with an attribute and no value
  • Implicit joins and Where in Doctrine - how?
  • Web.config system.webserver errors
  • Assign variable to the value in HTML
  • Content-Length header not returned from Pylons response
  • copying resource to sdcard gives a damaged file in android
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to add date and time under each post in guestbook in google app engine
  • MySQL WHERE-condition in procedure ignored
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Adding custom controls to a full screen movie
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • 0x202A in filename: Why?
  • Is there a mandatory requirement to switch app.yaml?
  • Comma separated Values
  • File upload with ng-file-upload throwing error
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • AngularJs get employee from factory
  • Error creating VM instance in Google Compute Engine
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Change div Background jquery
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make
  • Can't mass-assign protected attributes when import data from csv file
  • reshape alternating columns in less time and using less memory