473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 8165
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kiran J via JavaKB.com | last post by:
Hi, I have dynamically created a PDF document using PDF Box and I would like to store the PDDocument in database using blobs Could some one let me know how to convert the PDDocument into the...
6
by: Juergen Gerner | last post by:
Hello Python fans, I'm trying and searching for many days for an acceptable solution... without success. I want to store files in a database using BLOB fields. The database table has an ID field...
3
by: hamvil79 | last post by:
I'm implementig a java web application using MySQL as database. The main function of the application is basically to redistribuite documents. Those documents (PDF, DOC with an average size around...
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
6
by: (PeteCresswell) | last post by:
User wants to go this route instead of storing pointers in the DB and the documents outside. Only time I tried it was with only MS Word docs - and that was a loooong time ago - and it seemed to...
4
by: Bishman | last post by:
Hi, Can someone suggest the best technique / method of opening a Word ( or any other ) Document from an SQL BLOB ? I have the process of saving and retrieveing the file from an SQL Binary...
0
by: NM | last post by:
Hello, I've got a problem inserting binary objects into the postgres database. I have binary objects (e.g. images or smth else) of any size which I want to insert into the database. Funny is it...
4
by: lorirobn | last post by:
Hi, I need to add photos to my database. Back End is on MS SQL Server (I believe 2000), and Front End is on MS Access. I have read about storing the photos as BLOBS, but I am not sure how to...
3
by: Annonymous Coward | last post by:
I remember readng that BLOBs can be stored externally (with reference to the BLOB file stored in tables instead). Does anyone have any experience doing this ? I have a few questions: 1).what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.