I am uploading a image to back end and I want to display it on front end once the upload is complete. For doing this I am placing it under temp folder. The part I am unclear about it - how can I send this image to user browser?
-
2You should move the file to some directory readable by your web application and then display the image from there. – Manish Jan 07 '13 at 04:26
-
Whether the files are public? – Arun P Johny Jan 07 '13 at 04:27
-
Take look at this - http://stackoverflow.com/questions/2979758/writing-image-to-servlet-response-with-best-performance – Avinash T. Jan 07 '13 at 04:28
-
are you using servlets to upload file? – kaysush Jan 07 '13 at 05:06
-
Following thread will help you to solve this problem: http://stackoverflow.com/questions/2979758/writing-image-to-servlet-response-with-best-performance – Narendra Jan 07 '13 at 05:15
5 Answers
maybe you can use uploadify it is very easy and you can upload multiple files at once. Check the demos http://www.uploadify.com/demos/

- 611
- 3
- 6
In your HTML
file you need to use the img
tag. If your picture is titled pic.jpg
, you'd use something like:
<img src="temp/pic.jpg" alt="My incredible image.">

- 3,251
- 5
- 26
- 40
Servlet would be very helpful for this. You do not need to store the image to a folder or anything, use the below steps. You should get the image in bytes using "java.io.InputStream", and send the stream as bytes and should use java.io.OutputStream, to read it and display it. Please check Input stream javadoc and Output stream javadoc for more clarifications. To send the image from client side to server side use the image upload in a form element, and submit the form to your servlet and use inputstream to receive the image bytes and give it to outputstream and return an outputstream. The sample code to display the image in bytearray.
byte[] imageBytes = getImageAsBytes();
response.setContentType("image/jpeg");
response.setContentLength(imageBytes.length);
response.getOutputStream().write(imageBytes);
And finally give the url to your servlet returning an outputstream in the src attribute of the <img> tag <img src="url to your servlet">
You may also refer these.... 1.http://stackoverflow.com/a/1154279/1866662 2.http://stackoverflow.com/a/1264756/1866662

- 26
- 6
response.setContentType("image/jpg");
response.setHeader("Content-Disposition","inline;filename=filename.jpg");

- 1
Are you trying to open a local image in the default web browser? if so, do:
java.io.File imageFile = new java.io.File("image.png");
java.awt.Desktop.getDesktop().browse(imageFile.toURI());
It will open the File Image to the default web browser.

- 7
- 2