• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How do I get a byte-array to display the image in a browser ???

Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a .gif image stored in a byte array :
public byte[] picture;
I really do not want to write to a file first, and then use getImage() method to display the picture because it is an i/o overhead.
Are there any other ways of doing this ???
Author and all-around good cowpoke
Posts: 13078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically you want to have the image data served by a servlet from the byte array instead of the web server from a file. You have to arrange that the URL in the <img> tag points to a servlet that has the byte array. You have to set the response content-type to "image/gif" and the content-length to the byte[].length. See the HttpServletResponse API.
You MUST get an output stream instead of a writer so the bytes will go out unaltered. Then just write the byte array and close the stream.
This is a common item in many servlet books and you can find plenty of related discussions in older threads here.
    Bookmark Topic Watch Topic
  • New Topic