• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

displaying image from database

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


have written this code for displaying a image stored in the database but when i run the code it says
The image “http://localhost:8080/WebApplication8/displayserv” cannot be displayed, because it contains errors.

I would be pleased if someone could help ,,,
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you're trying to return several images in a single HTTP response; that doesn't work: 1 response = 1 image.

Delete the call to response.getWriter(); you can only call getWriter() or getOutputStream, not both.

You should also make sure that index == len; although that's likely to be the case, it's by no means guaranteed. You may have to call readImg.read in a loop to get all the bytes.
 
Ranga Medagoda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Thank you very much for your kind help,,, I changed the code according to your comment but still getting the same error
Is there any problem with content type,, I have a saved image (a gif) in the database (database is psql and data type used there is bytea) there is only one image for that id 'aa'

Thanks again for your help,,,

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For starters, drop the habit of putting QUOTE tags around your posts; as their name implies, they're for quoting something, not for new content.

Your code is confusing. You're retrieving a single attribute from the DB which you first treat as a string, and then as binary data. But it can only be one, not both - which one is it?

Make sure exactly as many bytes are streamed as there are in the image.

You can also use an HTTP monitor to look at what gets sent down the wire.

Lastly, " } catch (Exception e) { }" is a REALLY bad idea; how will you if there are problems?
 
Ranga Medagoda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thank you for your help again and sorry for the mistake done in quoting. I considered your comments and changed the code but still having the same problem. I think problem is with the second while loop which writes the data to the output stream but i need some help to rectify that...
Thanks for the help,
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Make sure exactly as many bytes are streamed as there are in the image.

You can also use an HTTP monitor to look at what gets sent down the wire.

 
Ranga Medagoda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found the error,, no problem with the reading bytes, problem was with the image inserted to the database. I used LargeObject data type instead of byteal this time, it worked.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey can you please help me i have same problem you solved please give me correct code
 
Sheriff
Posts: 67754
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
Welcome to the Ranch.

Niket Saini wrote:please give me correct code



That's not how things work around here. You write the code, and get help with it here.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic