【问题标题】:Jasper Report not getting the correct path贾斯珀报告没有得到正确的路径
【发布时间】:2014-12-26 21:04:30
【问题描述】:

我有一份 jasper 报告,想使用我的应用程序中的徽标 (gif)(在 /src/main/resources/img 中)

用于检索图像标志的代码是

public void imprimir(MyReport myreport) throws Exception    
{

    List myReportList = new ArrayList();

    File logo = new File(getClass().getClassLoader().getResource("img/myLogo.gif").getPath());
    myreport.setLogo(logo);
    myReportList.add(myreport);

    FileInputStream fis = (FileInputStream) getClass().getClassLoader().getResourceAsStream("jasper/myreport.jasper");
    // JasperReport report = JasperCompileManager.compileReport(fis);
    JasperPrint print = JasperFillManager.fillReport(fis, null, new JRBeanCollectionDataSource(myReportList));
    JasperExportManager.exportReportToPdfFile(print, "c:/myreport.pdf");

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JasperExportManager.exportReportToPdfStream(print, baos);

    DataSource datasource =  new ByteArrayDataSource(baos.toByteArray(), "application/pdf");

    Email mail = new Email();

    mail.setFromLabel("xxxxxxxx@xxxxxxxx.yyy.zz");
    mail.setTo("destiny@xxxxxxxx.yyy.zz");
    mail.setSubject("myreport");
    mail.setMessage("Mesage");

    EmailService emailService = new EmailService();
    emailService.sendEmail(mail, datasource);

}

但是这条路径不存在。

[Server:server01] 09:40:12,492 ERROR [stderr] (default task-3) Caused by: java.io.FileNotFoundException: C:\Java\AS\wildfly-8.1.0.Final\content\MyProject.war\WEB-INF\classes\img\logo.gif

因此,看起来,路径正在被解析为不同的值。 在域模式(集群)下通过 Wildfly 8.1 Final 进行部署。

我在这里错过了什么?

【问题讨论】:

    标签: java jasper-reports


    【解决方案1】:

    您的myLogo.gif 已打包在MyProject.war 文件中。路径C:\Java\AS\wildfly-8.1.0.Final\content\MyProject.war\WEB-INF\classes\img\logo.gif 不存在。

    我建议两种解决方案来解决这个问题。

    1.将myLogo.gif 移出MyProject.war。使用真实路径加载您的 gif 文件。

    File logo = new File(realPath);
    myreport.setLogo(logo);
    

    2.将myreport.setLogo(logo)方法的参数类型改为InputStream

    InputStream logoInputStream = getClass().getClassLoader().getResourceAsStream("img/myLogo.gif");
    myreport.setLogoInputStream(logoInputStream);
    

    【讨论】:

    • 我采用了不同的方法。我能够将徽标作为资源流读取,然后创建一个临时文件并使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多