• 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

Displaying image in JSP

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a column of bytea in my postgres table storing image.  The contents of the image is like "0xFFD8FFE000104A46494600010101006000600000FFDB0043000302020302020303030304030304050805050404050A070706080C0A0C0C0B0A0B0B0D0E1210...". I migrated this image from SQL server.

How I can display this image in my jsp. I tried Base64 encoded, but it is not working. When I inspect the contents of img tag, it is like "\x4e554c4c...."

Please help me.  
 
Saloon Keeper
Posts: 7582
176
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The img tag should contain an URL where the browser can find the image, not the image data itself. You'll need a servlet that serves just the binary image data using a unique URL containing the ID of the image.
 
Ranjan Yengkhom
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The same thing is working when I store a image with Base64 encoding.

my image tag contains like: <img  src="data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERETFhwX.." />

When I open the jsp, I bring the image as follows:

//ACTION CLASS
//set Photo working copy
BASE64Encoder base64Encoder = new BASE64Encoder();
StringBuilder imageString = new StringBuilder();
imageString.append("data:image/png;base64,");
imageString.append(new String(Base64.getDecoder().decode(rs.getBytes("Photo"))));
member.setPhoto(new String(imageString));

in jsp:

<img width="100" height="80" src="<s:property value='member.Photo'/>">

It works fine.
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, you're using a data: URL. OK, then: what format is the data that's stored in the DB? If it's ASCII and starts with "0x" then it's not in base64.
 
reply
    Bookmark Topic Watch Topic
  • New Topic