2013-07-26 47 views
0

我在JSF支持bean内部构​​建了一个图像格式的PDF文档,我需要在JSF页面中显示图像。我发现,primefaces具有名为组分,我定义的变量:将生成的png呈现到JSF中

private StreamedContent pdfImage; 

根据本示例http://www.primefaces.org/showcase/ui/dynamicImage.jsf。在我的代码 我内置的PDF使用一些数据和Apache PDFBox的和保存文档到:

private byte[] bytesPdf; 

我的JSF线

<p:graphicImage value="#{myBean.pdfImage}" rendered="#{myBean.showImage}"/> 

我叫下面是变换分析的第一个PDF文档页面,PNG方法之后我得到:

public void buildPDFImage() throws IOException{ 
    ByteArrayOutputStream os = new ByteArrayOutputStream(); 
    //Build bufferedImage from pdf bytearray 
    InputStream input = new ByteArrayInputStream(bytesPdf); 
    PDDocument archivo=new PDDocument(); 
    archivo=PDDocument.load(input); 
    PDPage firstPage = (PDPage) archivo.getDocumentCatalog().getAllPages().get(0); 
    BufferedImage bufferedImage = firstPage.convertToImage(); 
    //File exit=new File("d:/exit.png"); if i do something like this and pass file as param to iowrite image is generated on file system 
    try { 
     ImageIO.write(bufferedImage, "png", os); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }   
    pdfImage = new DefaultStreamedContent(new  ByteArrayInputStream(os.toByteArray()), "image/png");   
} 

当我运行我的应用程序生成PDF图像我得到这个跟踪器后

GRAVE: Error Rendering View[/pages/apphuella.xhtml] 
    java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed 
at org.apache.catalina.connector.Request.doGetSession(Request.java:2880) 
at org.apache.catalina.connector.Request.getSession(Request.java:2577) 
at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:920) 
at com.sun.faces.context.SessionMap.getSession(SessionMap.java:235) 
at com.sun.faces.context.SessionMap.put(SessionMap.java:126) 
at com.sun.faces.context.SessionMap.put(SessionMap.java:61) 
at org.primefaces.component.graphicimage.GraphicImageRenderer.getImageSrc(GraphicImageRenderer.java:105) 
at org.primefaces.component.graphicimage.GraphicImageRenderer.encodeEnd(GraphicImageRenderer.java:45) 
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875) 
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1763) 
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168) 
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845) 
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1756) 
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759) 
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759) 
at 

如果i'm正确使用,如果鸵鸟政策我如何使用它来显示生成的PNG(和其他图片)?如果还有其他there's选项,以显示对JSF页面运行时产生的图像我会知道的。

在此先感谢

+0

我记得我不得不把管Bean在会话范围。尝试这个 –

回答

0

首先,你需要保持该文件的临时目录。之后,你应该再次渲染文件。 尝试如下

辅助Bean

private String filePath; 

public String filePath() { 
    return filePath; 
} 

private String getSystemPath() { 
    Object context = getFacesContext().getExternalContext().getContext(); 
    String systemPath = ((ServletContext)context).getRealPath("/"); 
    return systemPath; 
} 

public void buildPDFImage() throws IOException { 
    // create any kind of file types. 
    byte[] cotent =        --> take byte array content of your files. 
    Stirng fileName = "xxx.pdf" or "xxx.png" --> your file name 
    String dirPath = "/pdf/" or "/images/";  --> a directory to place created files. 
    filePath = dirPath + fileName 
    createFile(new File(getSystemPath() + filePath), content); 
} 

private void createFile(File file, byte[] content) { 
    try { 
     /*At First : Create directory of target file*/ 
     String filePath = file.getPath(); 
     int lastIndex = filePath.lastIndexOf("\\") + 1; 
     FileUtils.forceMkdir(new File(filePath.substring(0, lastIndex))); 

     /*Create target file*/ 
     FileOutputStream outputStream = new FileOutputStream(file); 
     IOUtils.write(content, outputStream); 
     outputStream.flush(); 
     outputStream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

<h:form enctype="multipart/form-data"> 
    <h:commandButton value="Create"/> 
    <!-- Your Files is image --> 
    <p:graphicImage value="#{myBean.filePath}"/> 

    <!-- 
     Your Files is pdf. You need PDF Viewer plugin for your browser. 
     My FireFox vesion is 20.0. It have default PDF Viewer. 
     If PDF File cannot display on your browser, it is depond on browser setting also. 
    --> 
    <p:media value="#{myBean.filePath}" width="100%" height="300px"/> 
</h:form>