Read image from database

2007-04-11 Thread Zilberstein Yuval
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]



Re: Read image from database

2007-04-11 Thread Kris Schneider

Zilberstein Yuval wrote:

Hello,
Can anyone tell me how do I read an image from a Blob field in the
database? 


Not sure how this applies to taglibs, but you could look at:

http://java.sun.com/j2se/1.4.2/docs/api/javax/imageio/ImageIO.html#read(java.io.InputStream)


TX
YuvalZ


--
Kris Schneider mailto:[EMAIL PROTECTED]
D.O.Tech   http://www.dotech.com/

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Read image from database

2007-04-11 Thread Don Albertson

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.






-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Read image from database

2007-04-11 Thread Kris Schneider

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.


--
Kris Schneider mailto:[EMAIL PROTECTED]
D.O.Tech   http://www.dotech.com/

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Read image from database

2007-04-11 Thread Don Albertson

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. 


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Read image from database

2007-04-11 Thread Kris Schneider



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/

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Read image from database

2007-04-11 Thread Vikram Reddy

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();



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


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Read image from database

2007-04-11 Thread Kris Schneider

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/

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]