2013-02-21 85 views

回答

8

在瀏覽源代碼後,我找到了解決方案。

的JRXML imageExpression標籤:

<band height="79" splitType="Stretch"> 
<image scaleImage="FillFrame" isLazy="true" onErrorType="Blank"> 
    <reportElement uuid="3340bf0f-8471-45e9-8ea4-bdf44a7c0e68" x="0" y="0" width="150" height="69"/> 
    <imageExpression class="java.io.File"><![CDATA["image.jpg"]]></imageExpression> 
</image> 

Java代碼片斷:

FileResolver resolver = new FileResolver() { 
@Override 
public File resolveFile(String filename) { 
    return new File("/some/path"); 
} 
}; 


InputStream jasperfile = getClass().getClassLoader().getResourceAsStream("file.jasper"); 

LocalJasperReportsContext ctx = new LocalJasperReportsContext(DefaultJasperReportsContext.getInstance()); 
ctx.setClassLoader(getClass().getClassLoader()); 
ctx.setFileResolver(resolver); 
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperfile); 

JasperFillManager fillmgr = JasperFillManager.getInstance(ctx); 
JasperExportManager exmgr = JasperExportManager.getInstance(ctx); 

JasperPrint jasperPrint = fillmgr.fill(jasperReport, parameters, beanColDataSource); 
ByteArrayOutputStream pdfBytes = new ByteArrayOutputStream(); 
exmgr.exportToPdfStream(jasperPrint, pdfBytes); 

你必須創建一個新的上下文,並把它傳遞給JasperFillManager和JasperExportManager。

0

使用擴展是其他方式。

  1. 註冊表上jasperreports_extension.properties net.sf.jasperreports.extension.registry.factory.repository.th.resource.file.resolve = th.motive.jasper.report.extension.factory一個定製ExtensionsRegistryFactory。 ResourceFileResolveRepositoryExtensionsRegistryFactory

  2. 創建ExtensionRegistry呼叫ResourceFileResolveRepositoryExtensionRegistry

  3. 上ResourceFileResolveRepositoryExtensionRegistry可以註冊表服務來處理文件解析器

(*),它不僅是文件解析器,你可以做任何事情,StreamRepositoryService做

你從我的樣本基準全https://bitbucket.org/idplugin/th.motive.jasperreport.extend