I have a method that creates PDFs and a servlet that uploads files to my database. Is there anyway I could directly upload the created PDFs to my database? I am using FileOutputStream to create my PDFs.
Asked
Active
Viewed 609 times
0
-
2Might depend a bit on what kind of database you are using. – Scott Hunter Mar 25 '16 at 17:14
-
? do u mean by directly – manikant gautam Mar 25 '16 at 17:15
-
Hi Jane (if that is indeed your real name), you need to look at your database schema and find a binary object that can contain your PDF file. It might be a "BLOB" or something like that. Then you need to find a method in your db API that allows a file to be saved to that binary data type. – nicomp Mar 25 '16 at 17:17
-
Exactly, 1st question is what database are you using. Then, there's quite a few questions on the subject already, [this one](http://stackoverflow.com/questions/8952/storing-a-file-in-a-database-as-opposed-to-the-file-system) is about `sql-server`, [this one](http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay) is a long discussion whether it is worth it to store files on the database. Anyhow, as it is formulated right now, this question is too broad. You'll get much more chances of an answer if you narrow it down. – lrnzcig Mar 25 '16 at 17:18
-
I am gettings user's input and compiling them in a PDF so that I could make a file per user and store them in my database. Is there any way to create the PDF then store it in my database? Can I directly write my FileOutputStream to my database? I am using MS SQL SERVER. – Jane Doe Mar 25 '16 at 17:19
-
I already have a servlet that can upload files to my database manually. I would like a way to not have to upload them manually like could I just make the pdf and store it automatically to the database? – Jane Doe Mar 25 '16 at 17:21
-
Basically, how can I save my PDF directly to the database? – Jane Doe Mar 25 '16 at 17:27
-
You can simply get the byte[] generated and save it to the database. Since you are using SQL Server, you can save the byte[] to a `varbinary(n)`. Where n is the maximum size you expect them to be. If you need to store something big you can use `varbinary(max)`. – dambros Mar 25 '16 at 17:48
-
@JaneDoe you should not store your files in database, I recommend to use a datastore for files, or just store them in the file system. – Ben Mar 25 '16 at 18:23
-
See http://stackoverflow.com/a/6376516/545127 – Raedwald Mar 25 '16 at 18:31
-
@dambros how can I get the byte[] generated? – Jane Doe Mar 26 '16 at 07:35
1 Answers
1
Sounds like you need something like Java Caching System or Ehcache. Putting PDF's in a database is generally not a good idea, though it has certainly been done.
I suppose it depends on how long you expect the user to want to retrieve the file. If a long time, perhaps a database could have advantages because it is administered, backed up, etc. However, all the same things can be said about a file cache and if you have a lot of the files then you should have much happier system admins using a simple file store.

K.Nicholas
- 10,956
- 4
- 46
- 66