469,313 Members | 2,633 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,313 developers. It's quick & easy.

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

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,docDescriptio n,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.


Jul 17 '05 #1
2 7694
Tony wrote:
[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:

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,docDescriptio n,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.


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

--
XML is the programmer's duct tape.
Jul 17 '05 #2
Thanks Ray. I've solved the problem.

Tony
"Raymond DeCampo" <rd******@spam.twcny.spam.rr.spam.com.spam> wrote in
message news:Mi*******************@twister.nyroc.rr.com...
Tony wrote:
[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:

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,docDescriptio n,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.
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

--
XML is the programmer's duct tape.

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Kiran J via JavaKB.com | last post: by
6 posts views Thread by Juergen Gerner | last post: by
3 posts views Thread by hamvil79 | last post: by
6 posts views Thread by Kyle Teague | last post: by
4 posts views Thread by Bishman | last post: by
3 posts views Thread by Annonymous Coward | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.