2015-12-30 73 views
1

我想从数据库创建一个pdf文件,并且由于Apache PDF盒子只接受物理文件而图像部分处于卡住状态,因此我的数据库中的图像是blob格式。从数据库中获取图像到pdf文件

PDXObjectImage image = new PDJpeg(doc,rs.getBlob("image")); 

任何人都可以帮我吗?

+0

我会建议将图像存储在磁盘上,而不是存储有关图像在数据库中的元数据(即位置/名称等),除非您真的必须直接存储它们。看到这个线程,真的放在讨论上https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay?lq=1 – Tjernquist1

回答

1

将您斑点为InputStream,并通过这PDJpeg:

所有的
Blob imageBlob = rs.getBlob("image"); 
try (InputStream imageInputStream = imageBlob.getBinaryStream()) { 
    PDXObjectImage image = new PDJpeg(doc, imageInputStream); 
} 
+1

我可能是尼特采摘,但我会关闭该imageInputStream与尝试与资源:) – rjdkolb

+1

好点,我更新了我的答案 –

0

首先保存图像插入驱动器,然后添加此图片保存到您的报告。

 Connection  con = null; 
    Statement  st = null; 
    ResultSet  rs = null; 
    InputStream  is = null; 
    OutputStream os = null; 
    try { 
     Class.forName("oracle.jdbc.driver.OracleDriver"); 
     con = DriverManager. 
       getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name> 
        ,"user","password"); 
     st = con.createStatement(); 
     rs = st.executeQuery("select student_img from student_profile where id=101"); 
     if(rs.next()){ 
      is = rs.getBinaryStream(1); 
     } 
     is = new FileInputStream(new File("Student_img.jpg")); 
     os = new FileOutputStream("resourec/std_img.jpg"); 
     byte[] content = new byte[1024]; 
     int size = 0; 
     while((size = is.read(content)) != -1){ 
      os.write(content, 0, size); 
     } 
    }