Loading images from database

P

palak123i

Hi,

We have a requirement to load (show on screen) several images stored
in oracle table as BLOB. I am using code as follows to load the
images:

In JSP:

<img src='<%=request.getContextPath()%>/getImage.do?giftCode=10

In Action class:

I read image from database a binary stream and flush the image as
outputstream.

String giftCode =
request.getParameter("giftCode");

InputStream sImage;
sImage = rs.getBinaryStream(1);
String imageType = rs.getString(2);
response.reset();
response.setContentType(imageType);
response.addHeader("Content-Disposition","filename=getimage");
while((size=sImage.read(bytearray))!= -1 )
{
response.getOutputStream().write(bytearray,0,size);
}

response.flushBuffer();
sImage.close();

The problem is that, some of the images are not loading and show up as
'X' on UI. This behavior is intermittent.

Can anyone suggest a better way of showing images from database to UI?
Or any improvements in above code?

Thanks,
Palak
 
L

Lew

stefanomnn said:
Hi,
try casting ReslutSet to OracleResultSet, it gives you getBLOB()
method,

Please do not top post.

Is there a problem with using
i always used it and always worked.:

BLOB blob = ((OracleResultSet) rset).getBLOB (1);

Where should the OP find the BLOB type?

I would be astounded to find that there is such a type, or to discover that
there is a method OracleResultSet.getBLOB().
byte[] bytes = blob.getBytes (<begin index>, <length>);

The OP might prefer

InputStream image = rset.getBlob(1).getBinaryStream();

which uses interfaces, so does not tie them down to just the Oracle
implementation, and avoids knowing /a priori/ the size of the image to
retrieve, and is somewhat more flexible than using getBytes() (the Blob
method, not the BLOB method).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,059
Members
48,769
Latest member
Clifft

Latest Threads

Top