我发现这个问题有两种可能的解决方案:
解决方案 1:使用相对路径。
使用绝对路径可能不适用于您的服务器环境。因此,最好使用相对路径。最好将 'kh_logo.png' 文件与 .jrmxl 或 .jasper 文件放在同一文件夹中,并使用以下方式引用它:
<image>
<reportElement uuid="generated_uuid" x="8" y="9" width="170" height="51"/>
<imageExpression><![CDATA["kh_logo.png"]]></imageExpression>
</image>
如果这不起作用......
解决方案 2:使用文件解析器
此解决方案仅用于 Java 代码。在这里,您将自己的文件解析器作为参数传递给报告。像这样。。
///Jasper Resolver
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) {
e.printStackTrace();
return null;
}
}
};
parameters.put("REPORT_FILE_RESOLVER", fileResolver);
然后像上面那样引用它。
希望这可以帮助某人。