2013-02-27 68 views
0

我想将我的Java程序生成的PDF保存到Mysql数据库中,并试图在Java程序中检索/显示相同内容。下面的代码有什么我迄今所做的,从MySql/Java检索并显示PDF文档

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文件。

请帮助...

拉梅什

+0

你可以尝试[pdfOne](http://code.google.com/p/pdfonejava/),它有一个jar下载转换PDF格式为JPEG,如果GPL许可证适合你。 – 2013-02-27 11:56:51

+0

感谢您的建议,我正在尝试着解决这个问题。将更新你,如果它符合我的目的。 – 2013-02-27 12:06:14

回答

0

PDF不是图像格式 - 这是一个二进制格式。 您不能将它设置为您图片图标的图标。

+0

嗯,什么可能是一个合适的方法来解决这个问题呢? – 2013-02-27 11:37:53

+0

取决于你想要做什么?你需要将PDF图像设置为按钮图标吗?那么你需要得到一个PDF图像(只需谷歌或从adobe.com得到它) - 一旦拥有它,就像其他任何png/jpg一样。 – Jay 2013-02-27 13:55:06