java将PDF直接从数据库打开到用户的PDF应用程序中
我已经使用JavaFX创建了一个应用程序。我有一个PDF被上传到数据库,因为它的设计是为了在以后查看,并旨在使这个便携式(数据库和文件托管在服务器上)
目前,我的代码从存储在LongBLOB中的数据库中提取字节,并将其写入D:\驱动器上的一个文件
然后使用用户的默认PDF应用程序打开
有没有办法直接打开这个文件而不先写入文件?不希望用户必须下载文件才能查看
以下是查看PDF代码:
public void viewPdf(ActionEvent event) throws IOException, SQLException {
byte[] pdf;
Controller control = new Controller();
Connection con = control.connectDB();
PreparedStatement statement = con.prepareStatement("SELECT pdf, filename FROM files WHERE pdf_id =?");
statement.setInt(1, pdfId);
ResultSet rs = statement.executeQuery();
if (rs.next()) {
pdf = rs.getBytes("pdf");
filename = rs.getString("filename");
OutputStream newFile = new FileOutputStream("D:/pdfs/" + filename);
newFile.write(fileBytes);
newFile.close();
}
Desktop.getDesktop().open(new File("D:/pdfs/" + filename));
statement.close();
con.close();
}
共 (0) 个答案