Re: Read image from database

Kris Schneider <[email protected]>
Newsgroups gmane.comp.jakarta.taglibs.user
Message-ID <[email protected]>

Don Albertson wrote:
> Kris Schneider wrote:
> 
>> Don Albertson wrote:
>>
>>> Zilberstein Yuval wrote:
>>>
>>>> Hello,
>>>> Can anyone tell me how do I read an image from a Blob field in the
>>>> database?
>>>> TX
>>>> YuvalZ
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: [email protected]
>>>> For additional commands, e-mail: [email protected]
>>>>
>>>>
>>>>   
>>>
>>>
>>>
>>> I haven't put any of this into tags but....
>>>
>>> My experience is limited to our Oracle system. I created two classes:
>>>
>>> One wraps the Oracle implementation code and avoids the need to
>>> import oracle.sql.BLOB;
>>> import oracle.jdbc.OracleResultSet;
>>> in places that are isolated from driver implementation details
>>>
>>> Another converts the BLOB's to and from Image and byte[]  so I can 
>>> display them.
>>>
>>> For the web side, I adapted a servlet to display the image using this 
>>> fragment:
>>>
>>>                    byte[] imageData = image.getImageBytes();
>>>                    if ( imageData != null && imageData.length > 0){
>>>                        response.setContentType("image/jpeg");
>>>                        ServletOutputStream sos = 
>>> response.getOutputStream();
>>>                        sos.write(imageData);
>>>                    }
>>>
>>> I only save a thumbnail in the database as a BLOB so the thumbnail is 
>>> the only image being returned in the response stream.
>>
>>
>> If you're just serving the images out the database then there's 
>> certainly no need to create an "intermediate" Image (like my first 
>> reply) - just grab the byte[] and serve it with the proper content 
>> type. If the images are unlikely to change, I'd recommend also setting 
>> response headers so that the client knows it's OK to cache the images 
>> and can avoid making additional requests for them.
>>
>    The variable "image" isn't an Image, it's an instance of a class that 
> uses a BLOB and return it either as an Image or byte[].  In this case, 
> no Image is ever created.  To the servlet, "image" is an interface.

I got that. My first response in this thread included an example of 
creating a BufferedImage, which is what I was referring to.

-- 
Kris Schneider <mailto:[email protected]>
D.O.Tech       <http://www.dotech.com/>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.