Dileepa Dileepa - 4 months ago 15
Java Question

how to check whether file exists before using path in Jasper

I have a jrxml which has a node as follows,

<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?
If doesn't exists, how to fall back to a default image "default.jpg"?

Answer
<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

Comments