简体   繁体   English

如何在Jasper中使用路径之前检查文件是否存在

[英]how to check whether file exists before using path in Jasper

I have a jrxml which has a node as follows, 我有一个jrxml,它的节点如下,

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

How can I check whether physical file logo.jpg exists or not? 如何检查物理文件logo.jpg是否存在? If doesn't exists, how to fall back to a default image "default.jpg"? 如果不存在,如何退回到默认图像“ default.jpg”?

<variable name="LOGO" class="java.lang.String">
    <variableExpression>
        <![CDATA[(new File("logo.jpg").isFile())? "logo.jpg":"default.jpg"]]>
    </variableExpression>
    <initialValueExpression>
        <![CDATA[(new File("logo.jpg").isFile())? "logo.jpg":"default.jpg"]]>
    </initialValueExpression>
</variable>

<image>
  <reportElement x="181" y="0" width="209" height="74"/>
  <imageExpression class="java.lang.String"><![CDATA[$V{LOGO}]]></imageExpression>
</image>

Got clue from this site 从这个网站得到线索

You can use exists in the Java code, if that's what you're using to check with. 您可以使用Java代码中存在的代码(如果您要使用该代码进行检查)。 Not quite sure if you're asking for another approach that involves the XML itself. 不太确定是否要询问涉及XML本身的另一种方法。

If file doesn't exist, then you would just map the default image. 如果文件不存在,则只需映射默认图像。

Send your image path using parameter or field in jasper report. 使用Jasper报告中的参数或字段发送图像路径。 for example I am sending image path using field.Name of field is image imagePath . 例如,我使用field发送图像路径。字段名称是image imagePath If you have a image named logo.png , path of image as string other wise set path of default image. 如果您有一个名为logo.png的图像,则该图像的路径为字符串,否则将设置默认图像的路径。 and write this syntax with image. 并使用image编写此语法。

<image>
    <reportElement uuid="0db5b93f-46e4-4aaa-aac9-5a6d090d7eb6" x="127" y="23" width="206" height="78"/>
    <imageExpression><![CDATA[$F{imagePath}]]></imageExpression>
</image>

Enjoy... 请享用...

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

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