• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Streaming an image to the browser

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have image files that are BLOBS in a database, and I can load them into server memory (using a sql select).

I want to then display the image in the browser. I think response.getOutputStream() is involved somehow since JSPWriter is for text data. I am not sure if I can pass a byte arry directly into the html img tag (thinking outloud).

Any ideas?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See:
http://simple.souther.us


Look for SimpleStream
 
M Burke
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ben,
I wrote some code based on your example, but it does not work quite right.
The image fills the entire browser window and the html is somehow kicked out of the response buffer.

I place this code in the response html:
<img border="1" align="middle" src="profile001.jpg">

This code is called by a servlet to inject the picture into the response stream. It does send the pic to the browser, but it steps all over the html used to display it:

private void streamImg(HttpServletResponse response)throws ServletException, IOException{

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

BufferedInputStream bin = new BufferedInputStream(new FileInputStream("C:/a/profile001.jpg"));
ServletOutputStream sos = response.getOutputStream();

byte[] buffer = new byte[4 * 1024];

int data = 0;
while((data = bin.read(buffer)) != -1){
sos.write(buffer, 0, data);
}
sos.flush();
bin.close();

}
 
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sending the HTML and the image in the same response?

Your HTML should be the only thing sent in the first reponse, and the src attribute of the <img> tag should reference a servlet that only sends the image data.
 
M Burke
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I see Bear, thanks. It works now with a seperate servlet servicing the img tag
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic