简体   繁体   English

如何使用Java从MySql数据库中检索Blob pdf文件

[英]How to retrive Blob pdf file from MySql database using java

Hello guys I need help. 大家好,我需要帮助。 I have code below which saves the pdf file into mysql database it works perfectly, but i don't know how to retrieve the pdf file into my pc. 我下面有将pdf文件保存到mysql数据库中的代码,它可以正常工作,但是我不知道如何将pdf文件检索到我的pc中。 Any idea how to do it? 知道怎么做吗? Thanks 谢谢

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();
} 

请参阅Blob的API,尤其是getBinaryStream()方法,该方法可检索InputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM