2017-08-04 114 views
0

您好,我需要幫助。我有下面的代碼將pdf文件保存到mysql數據庫中,它完美的工作,但我不知道如何檢索到我的電腦的PDF文件。任何想法如何做到這一點?由於如何使用java從MySql數據庫中檢索Blob pdf文件

try{ 
//connection string 

File pdfFile = new File ("d:\\modularing.pdf"); 

byte[] pdfData = new byte[(int) pdfFile.length()]; 

DataInputStream dis = new DataInputStream(new FileInputStream(pdfFile)); 

dis.readFully(pdfData); // read from file into byte[] array 

dis.close(); 

PreparedStatement ps=con.prepareStatement("INSERT INTO filetable (" + "ID, " + 

"PDF_file " + ") VALUES (?,?)"); 

ps.setString(1, "newpdffile"); 

ps.setBytes(2, pdfData); // byte[] array 

ps.executeUpdate(); 

} catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

您應該使用[InputStream的(https://docs.oracle.com/javase/7 /docs/api/java/io/InputStream.html)是讀取blob的關鍵,請嘗試搜索API以讀取blob pdf文件。 –

回答

0

請參閱斑點的API,特別是getBinaryStream()方法,該方法獲取一個InputStream

相關問題