我想将我的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文件。
请帮助...
拉梅什
你可以尝试[pdfOne](http://code.google.com/p/pdfonejava/),它有一个jar下载转换PDF格式为JPEG,如果GPL许可证适合你。 – 2013-02-27 11:56:51
感谢您的建议,我正在尝试着解决这个问题。将更新你,如果它符合我的目的。 – 2013-02-27 12:06:14