从MySql/Java检索和显示PDF文档
我试图将Java程序生成的PDF保存到Mysql数据库中,并试图在Java程序中检索/显示相同的内容。下面的代码包含了我到目前为止所做的工作从MySql/Java检索和显示PDF文档,java,swing,jakarta-ee,jdbc,Java,Swing,Jakarta Ee,Jdbc,我试图将Java程序生成的PDF保存到Mysql数据库中,并试图在Java程序中检索/显示相同的内容。下面的代码包含了我到目前为止所做的工作 public class Save_PDFimage extends javax.swing.JFrame { private ImageIcon format = null; Connection conn = null; ResultSet rs=null; PreparedStatement pst=null;
public class Save_PDFimage extends javax.swing.JFrame {
private ImageIcon format = null;
Connection conn = null;
ResultSet rs=null;
PreparedStatement pst=null;
//String filename = "C:\\Users\\***\\Desktop\\n.png";
String filename = "C:\\Users\\***\\Desktop\\in.pdf";
int s = 0;
byte[] person_image = null;
private void Save_DBActionPerformed(java.awt.event.ActionEvent evt) {
try {
File image = new File(filename);
FileImageInputStream FIS = new FileImageInputStream(image);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for(int readNum; (readNum=FIS.read(buf))!=-1;) {
bos.write(buf,0,readNum);
}
person_image = bos.toByteArray();
} catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
try {
String sql = "Insert into invoices (Pdfs, Invoice_no_R) values(?,?)";
String i= "1";
pst = conn.prepareStatement(sql);
pst.setBytes(1, person_image);
pst.setString(2, i);
pst.execute();
} catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
// Code for displaying the retrieved PDF in a jLabel
private void Show_imageActionPerformed(java.awt.event.ActionEvent evt) {
try {
String sql = "select Pdfs from invoices where Invoice_no_R = 1";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if (rs.next())
{
byte[] Imagedata = rs.getBytes("Pdfs");
format = new ImageIcon(Imagedata);
jLabel1.setIcon(format);
}
} catch(Exception e ) {
JOptionPane.showMessageDialog(null, e);
e.printStackTrace();
} }
我可以将PDF保存在中,但程序在检索后不会显示图像,同样的逻辑适用于PNG/JPG文件
请帮忙
Rameshpdf不是图像格式,而是二进制格式。
您不能将其设置为图像图标的图标。hmm,那么什么是解决此问题的合适方法??取决于您正在尝试做什么?是否需要将pdf图像设置为按钮图标?然后你需要获得一个PDF图像(只需谷歌搜索或从adobe.com获取)-一旦你有了它,就可以像其他任何png/jpg一样设置它。如果GPL许可证对你合适,你可以尝试使用jar下载将PDF转换为JPEG。谢谢你的建议,我现在正在尝试解决这个问题。如果它符合我的目的,我会告诉你的。