0

我在 JSF 支持 bean 中构建了一个图像表单 PDF 文档,我需要在 JSF 页面中显示图像。我发现 primefaces 有一个名为 的组件,我定义了一个变量:

    private StreamedContent pdfImage;  

根据这个例子http://www.primefaces.org/showcase/ui/dynamicImage.jsf。在我的代码中,我使用一些数据和 apache PDFBox 构建了 pdf 并将文档保存到:

    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 

如果我不正确使用它,我会知道我是否正确使用它来显示生成的 png(和其他图像)?,以及是否有其他选项可以在 JSF 页面上显示运行时生成的图像。

提前致谢

4

1 回答 1

0

首先,您需要将文件保留为临时目录。之后,您应该再次渲染文件。尝试如下

支持豆

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>
于 2013-07-27T09:31:25.587 回答