问题
My programming language is java. My application is a web based application. I am using BIRT for generating pdf reports but the requirement is to automatically save them to the database after viewing the pdf report. In BIRT the pdf document is generated on the fly from the rpt templates. When user clicks on a URL the pdf document is displayed by the BIRT viewer application.
The requirement is once the pdf is generated i need to save a copy of the pdf file in the MySql database. This should happen automatically. once the user views the pdf (when the pdf is generated on the fly) the copy of the pdf file should get saved in the database.
Please let me know, how i can achieve this?
回答1:
It depends con how you use your database, direct jdbc, hibernate, ... But basically you have to do following:
- design an entity (and table) to hold the blob.
- don't use BLOB in MySQL is very small, better LONGBLOB.
- create a
java.sql.Blob
from file, usually you have to get an inputstream from file. - attach blob to hibernate entity, query param, ...
- persist.
I would rather prefer to store files in a different entity/table, and also store file name, size, MIME type, date, ... These attributes will be helpful later.
Use a 1:1 por 1:N relationship between the entity owning the file and the file entity.
Hope it helps!
回答2:
Adding to malaguna's answer, the key is to first store the PDF inside the DB and then serve it to the client. This requires basic knowledge of JDBC and web programming. Look at the source code of the servlet sample application (that you seem to be using). You just have to save the PDF output as a BLOB inside your database (as malaguna proposed) before returning it to the client.
来源:https://stackoverflow.com/questions/33064385/how-to-save-pdf-reports-automatically-in-database