AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Storing a dynamically created PDF file in a blob field

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases
View previous topic :: View next topic  
Author Message
Tony
Guest





PostPosted: Sun Aug 01, 2004 10:52 pm    Post subject: Storing a dynamically created PDF file in a blob field Reply with quote



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.




Back to top
Murray
Guest





PostPosted: Sun Aug 01, 2004 11:24 pm    Post subject: Re: Storing a dynamically created PDF file in a blob field Reply with quote




"Tony" <bones (AT) sprintmail (DOT) com> wrote

Quote:
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.

How about not even using a FileOutputStream? You're not writing to the
filesystem any more, so you shouldn't be using anything File-related.
There's two option I can think of:

- You can either write directly to the Blob's OutputStream
(Blob#setBinaryStream). This probably depends on the design of your code,
since anything blob-related needs to be done inside a transaction where a db
connection is already established (if I remember correctly?)

- Use a ByteArrayOutputStream and at the end of the PDF creation, get
the byte[] from it - ByteArrayOutputStream#toByteArray()



Back to top
Tony
Guest





PostPosted: Wed Aug 04, 2004 2:26 am    Post subject: Re: Storing a dynamically created PDF file in a blob field Reply with quote



Thanks Murray,

- Use a ByteArrayOutputStream and at the end of the PDF creation, get
the byte[] from it - ByteArrayOutputStream#toByteArray()

that was the solution.

regards.
Tony
"Murray" <parps (AT) SMAFFoffSPAMMER (DOT) optusnet.SPAMMAGE.com.au> wrote

Quote:

"Tony" <bones (AT) sprintmail (DOT) com> wrote in message
news:AuePc.22428$iK.11358 (AT) newsread2 (DOT) news.atl.earthlink.net...
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.

How about not even using a FileOutputStream? You're not writing to the
filesystem any more, so you shouldn't be using anything File-related.
There's two option I can think of:

- You can either write directly to the Blob's OutputStream
(Blob#setBinaryStream). This probably depends on the design of your code,
since anything blob-related needs to be done inside a transaction where a
db
connection is already established (if I remember correctly?)

- Use a ByteArrayOutputStream and at the end of the PDF creation, get
the byte[] from it - ByteArrayOutputStream#toByteArray()





Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.