Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Loading images from database

Thread view: 
palak123i@gmail.com - 11 Jun 2007 10:31 GMT
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
stefanomnn - 11 Jun 2007 14:59 GMT
Hi,
try casting ReslutSet to OracleResultSet, it gives you getBLOB()
method,
i always used it and always worked.:

BLOB blob = ((OracleResultSet) rset).getBLOB (1);
byte[] bytes = blob.getBytes (<begin index>, <length>);

Look at some examples here:
http://www.oracle.com/technology/sample_code/tech/java/sqlj_jdbc/htdocs/template
s.htm#LOBS

At the end, try adding Content-Length into response header.

Stefano

On 11 Giu, 11:31, "palak1...@gmail.com" <palak1...@gmail.com> wrote:
> Hi,
>
[quoted text clipped - 36 lines]
> Thanks,
> Palak
Lew - 11 Jun 2007 16:01 GMT
> Hi,
> try casting ReslutSet to OracleResultSet, it gives you getBLOB()
> method,

Please do not top post.

Is there a problem with using
<http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html#getBlob(java.lang.
String
)>
?

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

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.