如何在jasper报告上显示图像? [英] How to show an image on jasper report?

查看:169
本文介绍了如何在jasper报告上显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在碧玉报告上显示图像。我在.jrxml上有以下内容:

I want to show an image on a jasper report. I have the following on the .jrxml:

<image>
  <reportElement x="181" y="0" width="209" height="74"/>
  <imageExpression class="java.lang.String"><![CDATA["logo.jpg"]]></imageExpression>
</image>

图像logo.jpg与.jrxml位于同一目录中。只是说它不适合我。我google了一下,发现jasper报告考虑我放在.jrxml上作为JVM目录的相对路径,并且为了改变这一点,我需要传递一个返回文件的FileResolver作为REPORT_FILE_RESOLVER参数。所以,我在.java中做了以下内容(与.jrxml和图像位于同一位置)

The image logo.jpg is in the same directory as the .jrxml. By just putting that it didn't work for me. I googled a bit and found out that jasper report considers what i put on the .jrxml as a relative path to the JVM directory and that to change this I need to pass as a "REPORT_FILE_RESOLVER" parameter a FileResolver that returns the file. So, I did the following in my .java (is located in same place as the .jrxml and the image)

FileResolver fileResolver = new FileResolver() {

 @Override
 public File resolveFile(String fileName) {
  return new File(fileName);
 }
};
HashMap<String, Object> parameters = new HashMap<String, Object>();

parameters.put("REPORT_FILE_RESOLVER", fileResolver);
...

哪个应该返回预期的文件,但我仍然得到

Which should return the expected file, but I still get a

net.sf.jasperreports.engine.JRException: Error loading byte data : logo.jpg
    at net.sf.jasperreports.engine.util.JRLoader.loadBytes(JRLoader.java:301)
    at net.sf.jasperreports.engine.util.JRLoader.loadBytesFromLocation(JRLoader.java:479)
    at net.sf.jasperreports.engine.JRImageRenderer.getInstance(JRImageRenderer.java:180)
...

什么我做错了吗?

谢谢!

推荐答案

这是问题:

正如我先前所说,我在同一目录中有.jrxml,logo.jpg和使用.jrxml的.java。

As I said previously I have in the same directory the .jrxml, the logo.jpg and the .java that uses the .jrxml.

问题是fileResolver

The thing is that the fileResolver

FileResolver fileResolver = new FileResolver() {

 @Override
 public File resolveFile(String fileName) {
  return new File(fileName);
 }
};

未返回图像文件。我发现它映射到一个不同的目录而不是我期待的目录。因此,我将其更改为:

didn't returned the image file. I found out it mapped on to a different directory and not the one I was expecting. So, I changed it to:

FileResolver fileResolver = new FileResolver() {

     @Override
     public File resolveFile(String fileName) {
        URI uri;
        try {
          uri = new URI(this.getClass().getResource(fileName).getPath());
          return new File(uri.getPath());
        } catch (URISyntaxException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          return null;
        }
    }
};

这就结果了。我忘了这样一个事实:

And that worked out. I forgot the fact that:


相对路径名,相反,
必须用
信息来解释取自其他一些
路径名。默认情况下,
中的类java.io包总是解析当前
用户目录的
相对路径名。此目录是
,由系统属性user.dir,
命名,通常是
中的目录,Java虚拟机是
调用的。

A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

(摘自java api - 文件(Java Platform SE 6)

(taken from the java api - File (Java Platform SE 6))

调用JVM的目录不是我拥有的目录所有这些数据。

The directory in which the JVM is invoked is not the one I have all this data.

谢谢!

这篇关于如何在jasper报告上显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆