commons-fileuplaod for uploading files as blobs in oracle

E

Eqbal Z

I need to insert a row into an oracle table, one column of which is a
BLOB to save binary files. I am using JSP/Tomcat5/JDBC. Are there any
sample code or pointers in that direction on how to proceed on doing
this?

Thanks.
 
S

Sudsy

Eqbal said:
I need to insert a row into an oracle table, one column of which is a
BLOB to save binary files. I am using JSP/Tomcat5/JDBC. Are there any
sample code or pointers in that direction on how to proceed on doing
this?

This has almost reached FAQ status! The following link should
provide some guidance:
<http://www.sudsy.net/technology/clobs.html>
Even though it directly addresses CLOBs, BLOBs are essentially
the same.
 
E

Eqbal Z

Thanks for the link above. I forgot to mention, I am trying to use the
jakarta commons-fileupload package to read the file from the form. I
do not want to use byte array to update the BLOB field as memory
available to me is limited. How can I use the input stream I can get
from the fileupload utility to put it into the BLOB field?
 
S

Sudsy

Eqbal said:
Thanks for the link above. I forgot to mention, I am trying to use the
jakarta commons-fileupload package to read the file from the form. I
do not want to use byte array to update the BLOB field as memory
available to me is limited. How can I use the input stream I can get
from the fileupload utility to put it into the BLOB field?

Use BLOB#getBinaryOutputStream() to obtain an OutputStream then just
read from input stream and write to output stream. I'd suggest using
using something like this:

// assume inputStream is defined and open and rs is your
// OracleResultSet
byte buff[] = new byte[4096]; // just a 4K buffer
int rc; // number of characters read
int fieldNumber = 1; // insert the correct value here

BLOB blob = rs.getBLOB( fieldNumber );
OutputStream outputStream = blob.getBinaryOutputStream();
while( ( rc = inputStream.read( buff ) ) > 0 )
outputStream.write( buff, 0, rc );
outputStream.flush();
outputStream.close();
...
 
E

Eqbal Z

Ok, thanks again. I got stuck at another place now.
I get ClassCastExceptions when I try to do a (OraclePreparedStatement)
conn.prepareStatement(..), similarly for OracleResultSet.
Any idea why that could be. My connection is a sql connection obtained
from a JNDI resource (which has the oracle jdbc driver details). I am
on Tomcat.

Thanks.
 
E

Eqbal Z

Thanks for your help. I can upload files now. How do I now download it
to display in a browser? I know I have to get it into a BLOB field
then do some kind of streaming? How would I set the filename?
Any sample code?
 
S

Sudsy

Eqbal said:
Thanks for your help. I can upload files now. How do I now download it
to display in a browser? I know I have to get it into a BLOB field
then do some kind of streaming? How would I set the filename?
Any sample code?

THINK! That's what programming is all about. What if you were to
have a servlet which accepts query parameters? Perhaps the URL
would look something like this:
http://www.your.org/fetchServlet?id=xxx
Your doGet method could then retrieve the parameter by invoking
getQueryString on the HttpServletRequest.
So maybe you use the primary key of the database record as the
parameter. Then it's a simple matter of performing the database
lookup, getting the BLOB and invoking setContentType on the
HttpServletResponse. Oh, invoke setContentLength as well.
Stream the data from the BLOB to the client.
Now if you want me to program this all for you then we'd better
draw up a contract, therwise you're on your own. I've given
you more than enough information to get the job done.
If someone wants to give you code for free then that's their
choice...
 
E

Eqbal Z

Thanks again. I figured out how to download the files. The only thing
is, I have a file name field in my table, and I wanted the user to be
able to download with that file name. Right now the file name displays
like this when you try to download (display.jsp?id=xxx.pdf) But I will
try and figure out a way for it later. For now the project is
submitted for grading :).
 
M

Michael Borgwardt

Eqbal said:
Thanks again. I figured out how to download the files. The only thing
is, I have a file name field in my table, and I wanted the user to be
able to download with that file name.

You can do that via the Content-Disposition: HTTP header.
 
S

Sudsy

Eqbal said:
Thanks again. I figured out how to download the files. The only thing
is, I have a file name field in my table, and I wanted the user to be
able to download with that file name. Right now the file name displays
like this when you try to download (display.jsp?id=xxx.pdf) But I will
try and figure out a way for it later. For now the project is
submitted for grading :).

Good gracious, what have I done?!
Do you really want to be competing against this individual for
gainful employment?
Pillory me now, you regulars of the ng: I had no idea.
Should have done an nslookup of the IP address, I guess...
But doesn't the company claim competence in the technology?
Boy, do I feel like a SAP now...
 

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
473,765
Messages
2,569,568
Members
45,042
Latest member
icassiem

Latest Threads

Top