Reputation: 95
I'm trying to display images from a folder outside from my webapp folder with p:graphicImage inside a p:dataGrid but it does'nt work. However I wish to use URL's images for displaying them in another website. Here what I tried :
<!--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>
List of files :
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;
}
The image streamer :
@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());
}
}
Upvotes: 0
Views: 1569
Reputation: 1108722
You need to pass the image identifier (e.g. the unique filename as String
) via <f:param>
, not as a complete File
object via method argument. The image will namely be actually downloaded in a second, entirely independent, request. At that moment the getter will be invoked for a second time, however the method argument which is dependent on the p:dataGrid var
will not be available anymore. Instead, the argument needs to end up in URL of the image, that's exactly what the <f:param>
does.
Upvotes: 0