public StreamedContent getChart ( ) throws IOException { FacesContext context = FacesContext . getCurrentInstance ( ) ; if ( context . getCurrentPhaseId ( ) == PhaseId . RENDER_RESPONSE ) { // So, we're rendering the HTML. Return a stub StreamedContent so // that it will generate right URL. LOG . fine ( " return empty chart picture" ) ; return new DefaultStreamedContent ( ) ; } else { // So, browser is requesting the image. Return a real // StreamedContent with the image bytes. String chartId = context . getExternalContext ( ) . getRequestParameterMap ( ) . get ( "chartViewId" ) ; BufferedImage bufferedImg = ( BufferedImage ) FacesContext . getCurrentInstance ( ) . getExternalContext ( ) . getSessionMap ( ) . get ( chartId ) ; ByteArrayOutputStream os = new ByteArrayOutputStream ( ) ; ImageIO . write ( bufferedImg , "png" , os ) ; FacesContext . getCurrentInstance ( ) . getExternalContext ( ) . getSessionMap ( ) . remove ( chartId ) ; return new DefaultStreamedContent ( new ByteArrayInputStream ( os . toByteArray ( ) ) , "image/png" ) ; } }
Return real chart picture when browser requesting the image .