Re: Read image from database

Kris Schneider <[email protected]>
Newsgroups gmane.comp.jakarta.taglibs.user
Message-ID <[email protected]>
Vikram Reddy wrote:
> I think this should work :
> 
> 
>            InputStream input = null;
>           BufferedInputStream stream = null;
>              BLOB blob = null;
>              PrintWriter output = response.getWriter();
> 
>            if (res.next()) {
>                              blob = ( (OracleResultSet)
> rs).getBLOB("COLUMN_NAME");
>                                input = blob.getBinaryStream();
> 
>                               stream = new BufferedInputStream(input);
>                               int length = -1;
>                               while ((length = stream.read()) != -1) {
>                               output.write(length);
>                               }
>    }
>     /* flush the contents to the output */
>      output.flush();

That's pretty close to the approach that Don posted earlier. Couple of 
comments about the code:

The standard ResultSet supports both getBlob(int i) and getBlob(String 
colName). The standard Blob supports getBinaryStream(), as well as 
getBytes(long pos, int length). I don't see a need to use Oracle-specific APIs.

Not to be picky, but why would you assign the result of 
BufferedInputStream.read() to a var called "length"? It's just the next 
byte of data.

Just noticed that you're also reading from an InputStream and writing to a 
Writer. I'd recommend using ServletResponse.getOutputStream() instead.

> On 4/11/07, Zilberstein Yuval <[email protected]> wrote:
> 
>>
>> Hello,
>> Can anyone tell me how do I read an image from a Blob field in the
>> database?
>>
>> TX
>> YuvalZ

-- 
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.