This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes displaying image from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "displaying image from database" Watch "displaying image from database" New topic
Author

displaying image from database

Ranga Medagoda
Greenhorn

Joined: Sep 25, 2009
Posts: 6


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
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
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.


Ping & DNS - my free Android networking tools app
Ranga Medagoda
Greenhorn

Joined: Sep 25, 2009
Posts: 6



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
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
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

Joined: Sep 25, 2009
Posts: 6


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
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
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

Joined: Sep 25, 2009
Posts: 6
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

Joined: Aug 29, 2013
Posts: 1
Hey can you please help me i have same problem you solved please give me correct code
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: displaying image from database
 
Similar Threads
Download image(jpg format) using URL in jsp
display image after retrieving it from database?
Displaying an image from byte[] into JSP
servlet not running
Exception with displaying image