473,799 Members | 3,052 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 8252
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
3449
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
5709
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 and sort these parts by the ORDER field. Here's some pseudo code how I wanted to implement this...
3
4728
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 years. Those documents are mainly just accessed (update and delete are not so
6
3187
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 the overhead of serializing and unserializing data. -- Kyle
6
3202
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 appropriately (i.e. .DOC and .XLS).. but how about ..PDF? and can I stash a .TXT document in the...
4
5103
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 able to just open the file directly for viewing etc. I can 'trigger' a load for the file myself, but...
0
2707
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 type oid: "\074\077......";
4
3929
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 linking to the photo rather than storing it on
3
3321
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 data ? (An example would be very helpful) 3). How are errors handled in the stored proc that...
0
9541
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10225
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7564
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4139
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
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.