473,568 Members | 3,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 FileOutputStrea m
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.g etFile();

which returns an object.
then:
"INSERT INTO WPWDB.DOC_DOCUM ENT ("
+ "DOC_SUB_CAT_ID ,"
+ "DOC_DOC_NM_TXT ,"
+ "DOC_DOC_DESCN_ TXT,"
+ "DOC_ACTIVE_TXT ,"
+ "DOC_DOC_LNK_TX T,"
+ "DOC_DOC_MIME_T YP,"
+ "MODIFY_USUS_ID ,"
+ "MODIFY_DT_ TM)"
+ " VALUES (?,?,?,?,?,?,?, CURRENT TIMESTAMP)";

connection = getConnection() ;

pstmt = connection.prep areStatement(qu ery);
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.executeUp date();

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:

FileOutputStrea m fos = new FileOutputStrea m(docName);
private PdfWriter docWriter = null;
docWriter=PdfWr iter.getInstanc e(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.ad dDoc(subCatId,d ocName,docDescr iption,fileAsBy tes,
mimeType,modifi edUserID))

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

So my question is can anyone think of another approach to getting a
FileOutputStrea m into a format loadable in SQL using DB2???????
Thanks to any and all who can.


Jul 17 '05 #1
2 8194
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:

FileOutputStrea m fos = new FileOutputStrea m(docName);
private PdfWriter docWriter = null;
docWriter=PdfWr iter.getInstanc e(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.ad dDoc(subCatId,d ocName,docDescr iption,fileAsBy tes,
mimeType,modifi edUserID))

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

So my question is can anyone think of another approach to getting a
FileOutputStrea m into a format loadable in SQL using DB2???????
Thanks to any and all who can.


Tony,

You seem confused. First, a FileOutputStrea m 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
FileOutputStrea m writes to, use the File constructor that accepts a
String, passing the same name you passed to the FileOutputStrea m
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.s pam.com.spam> wrote in
message news:Mi******** ***********@twi ster.nyroc.rr.c om...
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:

FileOutputStrea m fos = new FileOutputStrea m(docName);
private PdfWriter docWriter = null;
docWriter=PdfWr iter.getInstanc e(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.ad dDoc(subCatId,d ocName,docDescr iption,fileAsBy tes,
mimeType,modifi edUserID))

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

So my question is can anyone think of another approach to getting a
FileOutputStrea m into a format loadable in SQL using DB2???????
Thanks to any and all who can.
Tony,

You seem confused. First, a FileOutputStrea m 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
FileOutputStrea m writes to, use the File constructor that accepts a
String, passing the same name you passed to the FileOutputStrea m
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
3421
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 inputstream -- Message posted via http://www.javakb.com
6
5671
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 (INT, auto_increment), an ORDER field (INT, for knowing the right order) and a "normal" BLOB field. It is planned to split large files in 64k-parts...
3
4708
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 2Mb) are stored in BLOB column. The amount of documents for the first year should not exceed 5/6 Giga, but I cannot make prevision for the next...
6
3164
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 entries with the entry in the other table? Having a separate table would result in two queries instead of one, but you wouldn't have to deal with...
6
3187
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 me like there were performance issues at the time. How about the different types? The MS docs I would expect Access to differentiate and handle...
4
5083
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 field working fine, but the user 'saves' the file from the Binary field to a binary disk file and can then open it. It would be better for them to be...
0
2690
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 works for files larger than 8000 Bytes. If a file is less than 1000 Bytes I get the following message: Error message: --invalid input syntax for...
4
3917
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 do this with SQL Server. Does this mean store the photo as OLE image, but do something else to it to make it a "Blob"? I have also read about...
3
3306
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 are the things to watch out for (apart from obvious ones like 'file not found' type errors). 2). How may a stored proc be written to fetch the BLOB...
0
7692
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7913
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6274
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2099
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.