4

I'm designing a jasper report using iReport which takes a parameter and fetches an image from a given URL:

The parameter is a user's screen name in twitter, and the url is it's profile image.

    <image>
       <reportElement x="4" y="51" width="73" height="64"/>
           <imageExpression><![CDATA["https://api.twitter.com/1/users/profile_image?screen_name="+$F{user_screen_name}+"&size=bigger"]]></imageExpression>
    </image>

It works great when the image exists. If it doesn't the following exception is thrown:

    Error filling print... net.sf.jasperreports.engine.JRException: Error opening input stream from URL :
    https://api.twitter.com/1/users/profile_image?screen_name=CPTCurtisHervey&size=bigger 
    Setting up the file resolver... net.sf.jasperreports.engine.JRRuntimeException:
    net.sf.jasperreports.engine.JRException: Error opening input stream from URL :
    https://api.twitter.com/1/users/profile_image?screen_name=CPTCurtisHervey&size=bigger 
    at net.sf.jasperreports.repo.DefaultRepositoryService.getInputStream(DefaultRepositoryService.java:138)
    at net.sf.jasperreports.repo.RepositoryUtil.findInputStream(RepositoryUtil.java:186)     
    at net.sf.jasperreports.repo.RepositoryUtil.getBytes(RepositoryUtil.java:202)
    at net.sf.jasperreports.engine.JRImageRenderer.getInstance(JRImageRenderer.java:141)     
    at net.sf.jasperreports.engine.fill.JRFillImage.evaluateImage(JRFillImage.java:498)
    at net.sf.jasperreports.engine.fill.JRFillImage.evaluate(JRFillImage.java:441)
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257)
    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:468)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2037)     
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:761)     
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportContent(JRVerticalFiller.java:291)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:133)     
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:903)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:813)
    at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:58)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:247)
    at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:878)
    at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)
    at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997) Caused by: 
    net.sf.jasperreports.engine.JRException: Error opening input stream from URL : 
    https://api.twitter.com/1/users/profile_image?screen_name=CPTCurtisHervey&size=bigger     
    at net.sf.jasperreports.engine.util.JRLoader.getInputStream(JRLoader.java:314)
    at net.sf.jasperreports.repo.DefaultRepositoryService.getInputStream(DefaultRepositoryService.java:121)
    ... 19 more Caused by: java.io.FileNotFoundException: 
    https://api.twitter.com/1/users/profile_image?screen_name=CPTCurtisHervey&size=bigger     
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1401)     
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at java.net.URL.openStream(URL.java:1029)     
    at net.sf.jasperreports.engine.util.JRLoader.getInputStream(JRLoader.java:310)... 20 more 
    Print not filled. Try to use an EmptyDataSource...

How can i handle this situation in the jrxml file?

I would like to simply add a fixed URL address in case it can't find one.

Anyone has any suggestions?

Thank you!

matino
  • 17,199
  • 8
  • 49
  • 58
Etay
  • 43
  • 1
  • 1
  • 6

3 Answers3

2

I think you need to add an additional helper class to handle this. You need a static method boolean urlExists(String url) that would allow you to put this in the imageExpression:

MyClass.urlExists($F{image_url}) ? $F{image_url} : $P{fallback_image}

It would be a simple class to write... but clearly there's additional complexity in adding in another .jar file. Without that method, I can't see any way to do the processing in the .jrxml.

mdahlman
  • 9,204
  • 4
  • 44
  • 72
0

In my case I had a report to which I "injected" the image's url and it was causing an error with the certificate (the url I was "injecting" had the IP and it had to be a name for the certificate). After I changed the url it worked.

Christian Vielma
  • 15,263
  • 12
  • 53
  • 60
0

in jrxml file, to show image from url resource, use image expression URL()

like below, for example $F{ADPN_NO} is parameter from report

<imageExpression><![CDATA[new URL("http://anyserver:7001/images/"+$F{ADPN_NO}+".jpg")]]></imageExpression>

when onError occurred, it will show blank item, icon, or error that is set on jrxml.

the other way, outside of jrxml file, you can use java map object which contains img urls that is check by URL() or File() Exception ..

joergl
  • 2,850
  • 4
  • 36
  • 42