简体   繁体   English

如何设置报表中图片的相对路径?

[英]How to set relative path of images in report?

So I have a java application that displays a jasper report. 所以我有一个显示jasper报告的Java应用程序。 There is an image I placed in the report using the iReport plugin for netbeans. 我使用用于Netbeans的iReport插件在报表中放置了一张图像。 Everything displays fine on my current machine but when I try to run the compiled jar on a different machine, the report won't load. 在当前计算机上,一切都可以正常显示,但是当我尝试在另一台计算机上运行已编译的jar时,报告将不会加载。

From looking at the windows console, I think it's because the path to the image is absolute, ie referencing a specific folder on the hard drive of the development machine. 从Windows控制台来看,我认为这是因为映像的路径是绝对的,即引用开发计算机硬盘上的特定文件夹。 I need to make it relative to the jar file. 我需要使其相对于jar文件。 I've placed the image into the package and have confirmed it's inside the compiled jar. 我已将图像放入包装中,并确认它在已编译的jar中。 But when I change the "image expression" value in iReport to "/reports/Logo.jpg" (where /reports is the package) and run the app I get 但是,当我将iReport中的“图像表达式”值更改为“ /reports/Logo.jpg”(其中/ reports是程序包)并运行该应用程序时,

EXCEPTION: Byte data not found at : /reports/Logo.jpgnet.sf.jasperreports.engine.JRException: Byte data not found at : reports/Logo.jpg

Any ideas what to do? 有什么想法怎么办? I am very stuck and would appreciate any help! 我非常困惑,不胜感激!

UPDATE: Got it. 更新:知道了。 Had to create a parameter in the report and called the parameter from the image expression. 不得不在报表中创建一个参数,并从图像表达式中调用该参数。 Then I created a HashMap and InputStream in the Java code and placed the input stream into the hash map! 然后,我用Java代码创建了一个HashMap和InputStream并将输入流放入哈希映射! So much code for so simple a thing! 这么简单的事情就需要这么多代码!

Java code : Java代码

        //to get images to display in report, pass their relative path as input stream and add to HashMap
        //there must be one stream and one HashMap per image
        InputStream imgInputStream = this.getClass().getResourceAsStream("/reports/omacLogo.jpg");
        InputStream imgInputStream2 = this.getClass().getResourceAsStream("/reports/omacLogo.jpg");
        parameters.put("omacLogo", imgInputStream);
        parameters2.put("omacLogo", imgInputStream2);

        InputStream jasper1 = this.getClass().getResourceAsStream("/reports/OmacYTDReportFinalpg1.jasper");
        InputStream jasper2 = this.getClass().getResourceAsStream("/reports/OmacYTDReportFinalpg2.jasper");

        JasperPrint jp1 = JasperFillManager.fillReport(jasper1, parameters,new JRBeanCollectionDataSource(ie.orderofmalta.BeanFactory.getCalcs()));
        JasperPrint jp2 = JasperFillManager.fillReport(jasper2, parameters2, new JRBeanCollectionDataSource(ie.orderofmalta.BeanFactory.getCalcs()));

Hope this helps someone else! 希望这对别人有帮助! Be aware you have to create separate hash maps and input streams for each image you want to place, even if it's the same image. 请注意,您必须为要放置的每个图像创建单独的哈希图和输入流,即使它是同一图像也是如此。

I've personally had not tried this way with a jar, but i hope it helps. 我个人还没有尝试过用罐子这种方式,但我希望它能对您有所帮助。 As you've stated the problem comes from the file path. 如前所述,问题出在文件路径。 On the iReport tool you can use relative paths and it works on the preview, but when the report generation is integrated within an application, it can only work with absolute paths. 在iReport工具上,您可以使用相对路径,并且可以在预览中使用,但是当报表生成集成在应用程序中时,它只能与绝对路径一起使用。
The way i've dealt with this drawback was by getting the absolute path of the image inside the java application, and passing it as a parameter to the report. 我解决此缺点的方法是通过获取Java应用程序中图像的绝对路径,并将其作为参数传递给报表。 Example: 例:

 String image = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/Cards_Template/front.jpg");

NOTE: I've built a JSF app, that's why i'm getting the path from it's context. 注意:我已经构建了一个JSF应用程序,这就是为什么我要从其上下文中获取路径。 If you don't, Java's IO or NIO API does have some methods to do the same. 如果不这样做,Java的IO或NIO API确实有一些方法可以做到这一点。 Basically, i get an absolute path from a relative path. 基本上,我从相对路径获得绝对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM