Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

displaying image from database

 
Ranga Medagoda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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 ,,,
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Niket Saini
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey can you please help me i have same problem you solved please give me correct code
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic