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 / April 2005

Tip: Looking for answers? Try searching our database.

Storing a dynamically created PDF file in memory and storing it to a blob field in a database

Thread view: 
Tony - 01 Aug 2004 23:53 GMT
Hi,
I have dynamically created a PDF document in memory as a FileOutputStream
Now I have to get it into a DB2 table, storing it as a BLOB. The table has a
document id,
document name, some date fields and this BLOB column that stores PDF Files.
Until now, the PDF files were read off of a disk drive. The code used was:

byte[] fileAsBytes = (byte[]) adminDocEvent.getFile();

which returns an object.
then:
"INSERT INTO WPWDB.DOC_DOCUMENT ("
+ "DOC_SUB_CAT_ID,"
+ "DOC_DOC_NM_TXT,"
+ "DOC_DOC_DESCN_TXT,"
+ "DOC_ACTIVE_TXT,"
+ "DOC_DOC_LNK_TXT,"
+ "DOC_DOC_MIME_TYP,"
+ "MODIFY_USUS_ID,"
+ "MODIFY_DT_TM)"
+ " VALUES (?,?,?,?,?,?,?,CURRENT TIMESTAMP)";

connection = getConnection();

pstmt = connection.prepareStatement(query);
pstmt.setInt(1, subCatId);
pstmt.setString(2, docName);
pstmt.setString(3, docDescription);
pstmt.setString(4, "Y");
==> pstmt.setBytes(5, fileasarray);
pstmt.setString(6, mimeType);
pstmt.setString(7, modfiedUserID);
pstmt.executeUpdate();

This is fine, if the pdf file exists as a file on a disk.
For my issue (loading a pdf file from memory) I use the following:

FileOutputStream fos = new FileOutputStream(docName);
private PdfWriter docWriter = null;
docWriter=PdfWriter.getInstance(pdfDocument, fos);
I then create the pdfDocument in memory using iText classes.
Then I try to prepare the file for the above sql stmt.

file = (Object) fos;
AdminDocDAO adminDocDAO = new AdminDocDAO();
if (adminDocDAO.addDoc(subCatId,docName,docDescription,fileAsBytes,
mimeType,modifiedUserID))

I get a casting exeception at: file = (Object) fos;

So my question is can anyone think of another approach to getting a
FileOutputStream into a format loadable in SQL using DB2???????
Thanks to any and all who can.
Raymond DeCampo - 02 Aug 2004 01:56 GMT
[snip]

> This is fine, if the pdf file exists as a file on a disk.
> For my issue (loading a pdf file from memory) I use the following:
[quoted text clipped - 15 lines]
> FileOutputStream into a format loadable in SQL using DB2???????
> Thanks to any and all who can.

Tony,

You seem confused.  First, a FileOutputStream is a mechanism for writing
bytes to a file.  It does not contain the bytes.  It can not be cast to
a File.  If you want a File object representing the file the
FileOutputStream writes to, use the File constructor that accepts a
String, passing the same name you passed to the FileOutputStream
constructor.  Then you can read in the bytes in the file into a byte array.

Note that this means you will have the entire file in memory.  If the
file is large, this is a bad idea.  Most JDBC drivers provide a means
for obtaining an OutputStream for BLOBs.  You should look into using
this mechanism.  In this case, you could skip the file, skip reading the
file into a byte array and just write the PDF directly to the database.
 (This assumes that you didn't need the file for other purposes.  If
you want the file too, you could still read a bit of the file, write it
to the db and repeat to avoid placing the entire file in memory.)

HTH,
Ray

Signature

XML is the programmer's duct tape.

Tony - 04 Aug 2004 03:21 GMT
Thanks Ray.  I've solved the problem.

Tony
> [snip]
> >
[quoted text clipped - 41 lines]
> --
> XML is the programmer's duct tape.
Kiran J - 18 Apr 2005 19:18 GMT
Tony,
Could you please let me how exactly you solved this issue.

Thanks and Regards
kiran


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



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