2013-07-23 117 views
2

我已將圖像保存爲我的oracle數據庫中的BLOB。我的模型類包含byte []圖像;對應於數據庫中的BLOB表達式。我必須將數據庫中的所有圖像導出爲PDF。在java中我用下面的代碼:在jasper報告中未顯示BLOB圖像pdf




    JasperReport jasperReport = JasperCompileManager.compileReport('jrxml file'); 
     JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(imageObjList); 
     //imageObjList containing the model 'ImageObj' which contain byte[] image 
     JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter,ds); 
     JasperExportManager.exportReportToPdfStream(jasperPrint,responce.getOutputStream()); 

我用iReport的創建JRXML文件

在我JRXML我創建了一個場與場類類型的java.io.InputStream

和圖像在我的圖像我給圖像表達式爲$ F {圖像},並已將圖像類表達式作爲java.awt.Image。

我無法做出我的pdf報告。

我得到一個例外,因爲




    net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression: 
     Source text : $F{image} 
     ..... 
     Caused by: java.lang.ClassCastException: [B cannot be cast to java.io.InputStream 
     at ImageReport_1374240048064_891215.evaluate(ImageReport_1374240048064_891215:171) 

我需要在PDF中的圖像。

回答

1

錯誤消息「[B不能轉換爲java.io.InputStream」意味着一個字節數組([B]不能轉換爲InputStream。問題在於,jrxml文件中的圖像字段的類型與ImageObj bean中的字段的類型不匹配。

根據JasperReports的終極指南可以使用以下類型的作爲輸入數據的圖像表達:

  • java.lang.String中
  • 的java.io.File
  • java.net。 URL
  • 的java.io.InputStream
  • java.awt.Image中
  • net.sf.jasperreports.engine.JRRenderable

所以你將不得不將你的byte []轉換爲另一個數據類型。 InputStream的是要走的最簡單的方法:

變化的吸氣劑ImageObj到:

public InputStream getImage() { 
    return new ByteArrayInputStream(image); 
} 

和圖像類表達式設置的java.io.InputStream。