如何在 Primefaces 中使用 graphicImage 从目录加载图像?
How to load an image from a directory using graphicImage in Primefaces?
大家好,我在从本地目录加载图像时遇到问题。
我依赖于从本地路径下载文件,但在这种情况下,图像显示在本地目录中。
在项目上下文之外:C:\image\fancoil-pared.ice.png
<p:graphicImage value="#{cMantConsultaProductosList.graphicImage}" />
管理:
@ManagedBean
@ViewScoped
public class CMantConsultaProductosList {
private StreamedContent graphicImage;
@PostConstruct
@Override
public void setViewAtributes() {
InputStream stream = new FileInputStream(new File("C:\imagen\fancoil-pared.ice.png"));
this.graphicImage = new DefaultStreamedContent(stream, "image/png", "fancoil-pared.ice.png");
}
/**
* @return the graphicImage
*/
public StreamedContent getGraphicImage() {
return graphicImage;
}
/**
* @param graphicImage the graphicImage to set
*/
public void setGraphicImage(StreamedContent graphicImage) {
this.graphicImage = graphicImage;
}
}
错误控制台:
org.primefaces.application.resource.StreamedContentHandler handle
Grave: Error in streaming dynamic resource. null
org.apache.catalina.core.StandardWrapperValve invoke
Grave: Servlet.service() para servlet Faces_Servlet lanzó excepción
java.io.IOException: java.lang.NullPointerException
at org.primefaces.application.resource.StreamedContentHandler.handle(StreamedContentHandler.java:78)
at org.primefaces.application.resource.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:72)
at javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:153)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:72)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
希望你能帮我解决这个问题,谢谢。
试试这个:
public StreamedContent getGraphicImage() {
return new org.primefaces.model.DefaultStreamedContent(
new java.io.FileInputStream("C:\imagen\fancoil-pared.ice.png"),
"image/png",
"fancoil-pared.ice.png"
);
}
正如 Oleg Tandrashko 在 PF forum
中所写
不支持 ViewScoped bean - 将通过独立于当前视图的自己的请求接收内容。
感谢朋友,我找到了解决方案,如果我确实必须将负载分离到另一个管理中,我将我的代码分享给社区。
@ManagedBean
public class CGraficoImagen {
public StreamedContent graphicImage;
public StreamedContent getGraphicImage() throws FileNotFoundException {
CMantConsultaProductosList rn = getCMantConsultaProductosList();
MProductoBean mantForm = (MProductoBean) rn.getFormMantenimiento();
StringBuilder archivoImagen = new StringBuilder();
archivoImagen.append(ReadParameterProperties.getRutaDirectorioImagen());
archivoImagen.append(mantForm.getVnomImagen());
return new DefaultStreamedContent(new FileInputStream(archivoImagen.toString()),
Constantes.MIME_IMAGE_PNG,
mantForm.getVnomImagen());
}
public void setGraphicImage(StreamedContent graphicImage) {
this.graphicImage = graphicImage;
}
private CMantConsultaProductosList getCMantConsultaProductosList() {
FacesContext ctx = FacesContext.getCurrentInstance();
return (CMantConsultaProductosList)ctx.getViewRoot().getViewMap().get("cMantConsultaProductosList");
}
}
大家好,我在从本地目录加载图像时遇到问题。
我依赖于从本地路径下载文件,但在这种情况下,图像显示在本地目录中。
在项目上下文之外:C:\image\fancoil-pared.ice.png
<p:graphicImage value="#{cMantConsultaProductosList.graphicImage}" />
管理:
@ManagedBean
@ViewScoped
public class CMantConsultaProductosList {
private StreamedContent graphicImage;
@PostConstruct
@Override
public void setViewAtributes() {
InputStream stream = new FileInputStream(new File("C:\imagen\fancoil-pared.ice.png"));
this.graphicImage = new DefaultStreamedContent(stream, "image/png", "fancoil-pared.ice.png");
}
/**
* @return the graphicImage
*/
public StreamedContent getGraphicImage() {
return graphicImage;
}
/**
* @param graphicImage the graphicImage to set
*/
public void setGraphicImage(StreamedContent graphicImage) {
this.graphicImage = graphicImage;
}
}
错误控制台:
org.primefaces.application.resource.StreamedContentHandler handle
Grave: Error in streaming dynamic resource. null
org.apache.catalina.core.StandardWrapperValve invoke
Grave: Servlet.service() para servlet Faces_Servlet lanzó excepción
java.io.IOException: java.lang.NullPointerException
at org.primefaces.application.resource.StreamedContentHandler.handle(StreamedContentHandler.java:78)
at org.primefaces.application.resource.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:72)
at javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:153)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:72)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
希望你能帮我解决这个问题,谢谢。
试试这个:
public StreamedContent getGraphicImage() {
return new org.primefaces.model.DefaultStreamedContent(
new java.io.FileInputStream("C:\imagen\fancoil-pared.ice.png"),
"image/png",
"fancoil-pared.ice.png"
);
}
正如 Oleg Tandrashko 在 PF forum
中所写不支持 ViewScoped bean - 将通过独立于当前视图的自己的请求接收内容。
感谢朋友,我找到了解决方案,如果我确实必须将负载分离到另一个管理中,我将我的代码分享给社区。
@ManagedBean
public class CGraficoImagen {
public StreamedContent graphicImage;
public StreamedContent getGraphicImage() throws FileNotFoundException {
CMantConsultaProductosList rn = getCMantConsultaProductosList();
MProductoBean mantForm = (MProductoBean) rn.getFormMantenimiento();
StringBuilder archivoImagen = new StringBuilder();
archivoImagen.append(ReadParameterProperties.getRutaDirectorioImagen());
archivoImagen.append(mantForm.getVnomImagen());
return new DefaultStreamedContent(new FileInputStream(archivoImagen.toString()),
Constantes.MIME_IMAGE_PNG,
mantForm.getVnomImagen());
}
public void setGraphicImage(StreamedContent graphicImage) {
this.graphicImage = graphicImage;
}
private CMantConsultaProductosList getCMantConsultaProductosList() {
FacesContext ctx = FacesContext.getCurrentInstance();
return (CMantConsultaProductosList)ctx.getViewRoot().getViewMap().get("cMantConsultaProductosList");
}
}