从项目文件夹中加载图像,并在jsf弹出窗口中显示

时间:2015-11-19 13:57:57

标签: java bytearray inputstream

在我的要求中,我的项目文件夹中有一个图像,如果数据库不包含图像我在弹出窗口中显示默认图像

默认图片未显示其显示为空  please refer image

每件事都适用于数据库图像   working image form database

单击PDF按钮后,它执行以下方法:

public void getImage(ExpenseTO to) {
        ExpenseTO fetchEntityById = accountService.fetchEntityById(
                ExpenseTO.class, to.getExpenseId());
        byte[] fileScanned = fetchEntityById.getUploadedScanedFile();
        if (fileScanned.length <= 20) {
            try {
                ClassLoader classLoader = Thread.currentThread()
                        .getContextClassLoader();
                InputStream input = classLoader.getResourceAsStream("file.png");
                byte[] bytes = IOUtils.toByteArray(input);
                outPutPdfToDisplay = new DefaultStreamedContent(
                        new ByteArrayInputStream(bytes));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            outPutPdfToDisplay = new DefaultStreamedContent(
                    new ByteArrayInputStream(fileScanned));
        }
    }

它执行任何异常但不显示
 弹出窗口中的图片

1 个答案:

答案 0 :(得分:0)

最后我得到了解决方案

在JSF的帮助下

听到我的代码:

public void getImage(ExpenseTO to) {
        ExpenseTO fetchEntityById = accountService.fetchEntityById(
                ExpenseTO.class, to.getExpenseId());
        byte[] fileScanned = fetchEntityById.getUploadedScanedFile();
        if (fileScanned.length <= 20) {                     
                outPutPdfToDisplay=null;            
        } else {
            outPutPdfToDisplay = new DefaultStreamedContent(
                    new ByteArrayInputStream(fileScanned));
            }
    }  

,对话框为:

<p:dialog widgetVar="test" id="dialog" position="300,10"  draggable="true"  modal="true" resizable="false" closable="true">
                    <p:media rendered="#{accountsHomeBean.outPutPdfToDisplay != null}" value="#{accountsHomeBean.outPutPdfToDisplay}" width="700px" height="700px" player="pdf" cache="false" />
                    <h:graphicImage value="/images/file.png"   rendered="#{accountsHomeBean.outPutPdfToDisplay == null}"/>
                </p:dialog>

如果图像存在于数据库<p:media>中,则执行标记 否则如果image在数据库中为null,它将执行<h:graphicImages>

its look like

相关问题