Basically, just create Servletone that receives InputStreamfrom it with the help FileInputStreamand writes it to OutputStreamin HttpServletResponsetogether with the correct set of response headers, at least with content-type. Finally, call this servlet in the srcelement attribute <img>along with the file identifier as a request parameter or pathinfo. For instance:.
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) {}
}
Here you can find the complete basic example: http://balusc.blogspot.com/2007/04/imageservlet.html
source
share