我正在使用iReport设计一个jasper报告,该报告接受一个参数并从给定的URL中获取图像:

该参数是用户在twitter中的屏幕名称,而url是其个人资料图像。

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


当图像存在时,效果很好。如果不是,则抛出以下异常:

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


如何在jrxml文件中处理这种情况?

我想简单地添加一个固定的URL地址,以防找不到。

有人有建议吗?

谢谢!

最佳答案

我认为您需要添加一个额外的帮助程序类来处理此问题。您需要一个静态方法boolean urlExists(String url),该方法可以将其放入imageExpression中:

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


这将是一个简单的类编写...但是显然,添加另一个.jar文件会带来额外的复杂性。如果没有该方法,则无法在.jrxml中看到任何处理方式。

10-03 00:44