This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JSP and the fly likes Fetching image from database Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "Fetching image from database" Watch "Fetching image from database" New topic
Author

Fetching image from database

Boon Subra
Ranch Hand

Joined: Jul 18, 2001
Posts: 69
Hi all,
I am trying to fetch an image from the database and display it in a JSP page. The byte output is printing. Please help to convet the value and display it in the browser.
The code is as follows.
if(rs.next())
{
BufferedInputStream bins = new BufferedInputStream(rs.getBinaryStream(1));
byte[] buf = new byte[4*1024];
int len;
while((len= bins.read(buf,0,buf.length))!=-1)
{
out.write(buf,0,len); // the output is getting printed here.
}
How to display it in the browser.
Thanks,
Bhuvana
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
After you read the image to the JSP, I think you need to write it to the file system, that can be accessed by public.

Does this what you need?
Nick.
[ March 11, 2004: Message edited by: Nicholas Cheung ]
[ March 11, 2004: Message edited by: Nicholas Cheung ]

SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56205
    
  13

There is no need to write the image to a file. Your best bet would be to code up a servlet that retrieves the image bytes from the DB and returns them as the response, with the appropriate content type and lengths specified.
Then in your JSP you would use an img tag just like any other image, setting the src attribute to the URL of the servlet.
[ March 14, 2004: Message edited by: Bear Bibeault ]

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
What you're doing is attempting to mix text and binary output in a single servlet response.
This is impossible. The content type is set only once (any new set would override that) and sent to the client in the HTTP headers.
Therefore your image is sent as a byte stream which the browser will attempt to interpret as HTML or clear text and dump to the screen.
Do as Bear said and code it as a servlet that sets the contenttype to jpeg (or whatever image format you use) and call that from an img tag in your JSP.


42
Boon Subra
Ranch Hand

Joined: Jul 18, 2001
Posts: 69
Hi Bear and Jeroen,
Thanks a lot for the suggestion could you send how exactly I should write in the JSP page by setting the context type.
Thanks
Bhuvana
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56205
    
  13

Sorry that should have been content type.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Fetching image from database
 
Similar Threads
Problem creating the zip file
BufferedInputStream.read method
Display Image in JSP using servlet
BufferedInputStream(Very Uregent)
Problem with servlet reading images in database!