• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Streaming an image to the browser

 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • 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?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
M Burke
Ranch Hand
Posts: 406
  • Mark post as helpful
  • send pies
  • 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();

}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64992
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I see Bear, thanks. It works now with a seperate servlet servicing the img tag
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic