如何从MYSQL db中检索PDF文件

时间:2018-04-06 09:43:09

标签: java mysql database pdf jtable

正如标题中所写的那样,我真的不知道怎么做,我尝试了这个代码,它只允许我插入pdf而不是检索它。

 public class PDFManager extends javax.swing.JFrame {
   //private PDFParser parser;
   //private PDFTextStripper pdfStripper;
   //private PDDocument pdDoc;
   //private COSDocument cosDoc;

   //private String Text, fileName;
   private String filePath, author, title;
   private File file;
   private int numberOfPages;
   private Connection con; 
   private Statement stmt;
   private ResultSet rs;
   String Query; 
   PreparedStatement pstmt;



   private void DatabaseConnect() {
        try {
            Class.forName("com.mysql.jdbc.Driver");

            String host = "jdbc:mysql://localhost:3306/mg-marjieidb";
            String username = "root";
            String password = ""; 
            String unicode= "?useUnicode=yes&characterEncoding=UTF-8";
            con = DriverManager.getConnection( host+unicode, username, password );
            stmt = con.createStatement();

        } catch ( ClassNotFoundException | SQLException err ) {
          System.out.println( err.getMessage( ) ); }
    }

    public PDFManager() {
       DatabaseConnect(); 
    }
   public void ToText() throws IOException
   {
       //this.pdfStripper = null;
       //this.pdDoc = null;
       //this.cosDoc = null;

       file = new File(filePath);

       //parser = new PDFParser(new RandomAccessFile(file,"r")); // update for PDFBox V 2.0
       //parser.parse();
       //cosDoc = parser.getDocument();
       //pdfStripper = new PDFTextStripper();
       //pdDoc = new PDDocument(cosDoc);
       //pdDoc.getNumberOfPages();
       //pdfStripper.setStartPage(1);
       //pdfStripper.setEndPage(10);

       PDDocument document = PDDocument.load(file);
       PDDocumentInformation pdd = document.getDocumentInformation();
       author = pdd.getAuthor();
       title = pdd.getTitle();
       numberOfPages = document.getNumberOfPages();

       //reading text from page 1 to 10
       // if you want to get text from full pdf file use this code
       // pdfStripper.setEndPage(pdDoc.getNumberOfPages());  
       //Text = pdfStripper.getText(pdDoc);
       if(author == null){
           author = "";
       }
       else if(title == null){
           title = "";
       }
       // Code to insert book information here.
             try 
              {

                 FileInputStream fis = new FileInputStream(file);
                 int len = (int)file.length();
                 // insert pdf file to db
                 Query = ("INSERT referenceFile INTO referencedocument (documentType, title, author, pages, referenceFile)"+" VALUES(?, ?, ? ,? ,?)");
                 pstmt = con.prepareStatement(Query);
                 pstmt.setString(1, "book");
                 pstmt.setString(2, title);
                 pstmt.setString(3, author);
                 pstmt.setInt(4, numberOfPages);
                 // method to insert a stream of bytes
                 pstmt.setBinaryStream(5, fis, len); 
                 pstmt.executeUpdate();
                 rs = stmt.executeQuery("SELECT documentID FROM referencedocument WHERE title = '"+ title +"' AND author = '"+ author +"'");
                 rs.next();
                 int id = rs.getInt("documentID");
                 stmt.executeUpdate("INSERT INTO book (documentID, edition)"+" VALUES ( '"+ id +"', '"+0+"')");
                //*************************************************************
                File f = new File("m.pdf");
                FileOutputStream output =  new FileOutputStream(f);
                  Library l;
                 l=new Library(); 
                TableModel model = l.DocsList.getModel();
                // DefaultTableModel model = (DefaultTableModel) l.DocsList.getModel();
                 JTableHeader header = l.DocsList.getTableHeader();
                 int i =(int) l.DocsList.getSelectedRow();
                Connection con = l.DBConnection();
                 int ID = (int) model.getValueAt(i, 5);
                 //int id = rs.getInt("documentID");
             rs = stmt.executeQuery("SELECT referenceFile FROM referencedocument WHERE documentID = "+ID);
                //rs = stmt.executeQuery("SELECT referenceFile FROM referencedocument WHERE title = '"+ title +"' AND author = '"+ author +"'");
                if(rs.next()){
                    InputStream input = rs.getBinaryStream("referenceFile");
                    byte [] buffer = new byte[1024];
                    while(input.read(buffer) > 0){
                        output.write(buffer);
                    }
                }
              } catch (SQLException ex)
                  {
                    System.out.println(ex.getMessage());
                  }
   }
   public String getTitle(){
       return title;
   }
   public int getNumberOfPages(){
       return numberOfPages;
   }
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    } 
}

我想知道它为什么不起作用? :"""&#34)

0 个答案:

没有答案