2013-07-26 1 views
0

JSF 백업 빈 내에 이미지 양식 PDF 문서를 만들었습니다. JSF 페이지 안에 이미지를 표시해야합니다. 본 실시 예에 따른 http://www.primefaces.org/showcase/ui/dynamicImage.jsf렌더링이 JSF에 png 생성

private StreamedContent pdfImage; 

: I는 primefaces라는 요소를 가지고, I 변수를 정의 알았다.

private byte[] bytesPdf; 

내 JSF 라인은 내가 PNG에 처음으로 PDF 문서의 페이지를 tranform 방법에 따라 호출 그 후

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

입니다 : 내 코드 에서 나는로 문서를 일부 데이터와 아파치 PDFBox를 사용하여 PDF로 저장을 내장 내가 얻을 :

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 페이지에 런타임 생성 이미지를 보여줄 다른 옵션이 있는지 알고 싶습니다.

미리 감사드립니다.

+0

내가 세션 범위에서 관리되는 콩을 넣어했던 기억을 백업

다음과 같이하십시오. 이 시도 –

답변

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>