0

ap:dataGrid 内の p:graphicImage を使用して、webapp フォルダー外のフォルダーから画像を表示しようとしていますが、機能しません。ただし、URL の画像を別の Web サイトに表示するために使用したいと考えています。ここで私が試したこと:

        <!--works-->
        <p:graphicImage value="#{imageStreamer.getStreamedImage(fileManagerBean.resources.get(0))}" width="100"/>
        <!--does'nt work-->
        <p:dataGrid id="dataGrid" var="file" value="#{fileManagerBean.resources}" >
            <p:commandLink action="#{fileManagerBean.setFicher(file)}" onclick="dialog.show();" update=":img,:url">
                <p:graphicImage value="#{imageStreamer.getStreamedImage(file)}" width="100"/><br/>
                <h:outputText value="#{file.name}" />
            </p:commandLink>
        </p:dataGrid>

ファイルのリスト:

public List<File> getResources() {
        String path = "/opt/www/images";
        File resourceDirectory = new File(path);
        String[] extensions = {"png", "jpg", "jpeg", "gif"};
        Collection<File> files = FileUtils.listFiles(resourceDirectory, extensions, true);
//        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
//        Set<String> resources = context.getResourcePaths("/images");
        List<File> resources = new ArrayList<File>();
        for (File resource : files) {
            resources.add(resource);
        }
        return resources;
    }

イメージストリーマー:

@ManagedBean
@ApplicationScoped
public class ImageStreamer {

    public StreamedContent getStreamedImage(File file) {
        InputStream stream = null;
        String mimeType = null;
        try {
            stream = new FileInputStream(file);
            mimeType = URLConnection.guessContentTypeFromStream(stream);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ImageStreamer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ImageStreamer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new DefaultStreamedContent(stream, mimeType, file.getName());
    }
}
4

1 に答える 1