I need to display pictures (to the client browser) that are located elsewhere than in my application directory.

Joost Schouten

The code below, will get your image, and sent it to the clients browser. It is up to the browser to decide how to display it. If it is an jpg or gif, most browsers will display it. Otherwise the user will be prompted where to save the image. If you want your image displayed within an HTML file. Store the file temporarely within the root (eg in a folder with a random name). And have the html refer to the image. delete the image after some time. This way it is available for a little. The second solution is not completely safe, since your file is available for anyone for a little bit. But it is considered reasonably safe, since it is a method used by several majour sites. I believe Hotmail handles attachments like that.

File file = new File("realPathToYourFile);
FileInputStream fileIn = new FileInputStream(file);
byte[] b = new byte[fileIn.available()];
fileIn.read(b);
fileIn.close();
ServletOutputStream downLoad = response.getOutputStream();
downLoad.write(b);
response.getOutputStream().flush();
Make sure you do not call the outputStream on the response or forward the response to another servlet or jsp.

Good luck
Joost Schouten

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact