12 Replies Latest reply on Feb 8, 2011 8:27 AM by micnight

    Using a4j:mediaOutput to display a JPEG from a BLOB

    notify

      I have successfully used rich:fileUpload and a4j:mediaOutput to upload a JPEG from my local drive, preview it on the web page then persist it using JPA and an EJB into a MySQL table of type BLOB.

       

      I am now trying to display the the JPEG on another page.

       

      I read the contents of the table into seperate Entity Beans in a list. I iterate through the list and dislay the date/time stamp, description of the JPEG but the actual JPEG is just displayed as a broken link/"X" in the image style (style="width:100px; height:100px;").

       

      Any examples of reading a BLOB and displaying it using a4j:mediaOutput.

       

      I have tried passing the entities reference to the JPEG (BLOB) back to the Bean which does the preview paint, but no joy with that either.

       

      Thanks.

        • 1. Re: Using a4j:mediaOutput to display a JPEG from a BLOB
          ilya_shaikovsky

          using mediaOutput you just passing the image data to Stream. So seems something wrong with your "paint" method as mediaOutput really doesn't care from which source you will get the data you passing to stream.

          • 2. Re: Using a4j:mediaOutput to display a JPEG from a BLOB
            notify

            When I use rich:fileUpload and then preview the uploaded file, from the RF example page everything works fine. Will look at paint method.

             

            <rich:fileUpload fileUploadListener="#{fileUploadBean.listener}"

            maxFilesQuantity="#{fileUploadBean.getUploadsAvailable()}"

            id="upload"

            immediateUpload="#{fileUploadBean.isAutoUpload()}"

            acceptedTypes="jpg" allowFlash="#{fileUploadBean.isUseFlash()}">

            <a4j:support event="onuploadcomplete" reRender="info" />

            </rich:fileUpload>

             

            Bean's paint;

             

            public

            void paint(OutputStream stream, Object object) throws IOException {

            stream.write(getFiles().get((Integer) object).getData());

            }

             

             

             

             

             

             

            • 3. Re: Using a4j:mediaOutput to display a JPEG from a BLOB
              ilya_shaikovsky

              seems that getFiles().get((Integer) object).getData() not returns data properly. check object scope.

              • 4. Re: Using a4j:mediaOutput to display a JPEG from a BLOB
                notify

                As the images are in the Entity Bean returned as a collection from my EJB JPA call to the table should I change the paint method to refect this then?

                 

                The stream.write should write out my Postcard entity i.e

                 

                postcard.getImage();

                 

                 

                • 5. Re: Using a4j:mediaOutput to display a JPEG from a BLOB
                  ilya_shaikovsky

                  I just mean that you should check in debug if the objects which should be passed to Stream actually correct when it called.

                  • 6. Re: Using a4j:mediaOutput to display a JPEG from a BLOB
                    notify

                    Ilya,

                     

                    It looks like the a4j:mediaOutput is unable to pass the reference to the BLOB (byte []) from my Entity Bean. The call to a4j:mediaOutput is inside a rich:dataTable.

                     

                    If I just display the BLOB (no call to a4j:mediaOutput) i get the refence to it displayed.

                     

                    I pass something else and it calls the method (fileUploadBean.paint), all be it null.

                     

                    Is this a known issue?

                     

                    Thanks.

                     

                    <

                    rich:dataTable id="dataTableYourPhotographs" value="#{postcardsList}" var="postcards" columns="4" rendered="#{postcardsList.size() > 0}" >

                     

                    <rich:column style="text-align: center;">

                     

                    <f:facet name="header">Date/Time Added</f:facet>

                     

                    <h:outputText value="#{postcards.dateOfImage}" >

                     

                    <f:convertDateTime pattern="dd/MM/yyyy HH:mm" timeZone="Europe/London" />

                     

                    </h:outputText>

                     

                    </rich:column>

                     

                     

                    <rich:column>

                     

                    <f:facet name="header">Location</f:facet>

                     

                    <h:outputText value="#{postcards.googleMapsReverseGeocodeAddress}" />

                     

                    </rich:column>

                     

                     

                    <rich:column>

                     

                    <f:facet name="header">Postcard</f:facet>

                     

                    <a4j:mediaOutput element="img" mimeType="image/jpeg"

                     

                    createContent="#{fileUploadBean.paint}" value="#{postcards.image}"

                     

                    style="width:100px; height:100px;" cacheable="false">

                     

                    </a4j:mediaOutput>

                     

                    </rich:column>

                     

                     

                    <rich:column style="text-align: center;">

                     

                    <div class="actionButtons">

                     

                    <a4j:commandButton action="#{geofenceBean.deletePostcard}" value="Delete Postcard" reRender="dataTableYourPhotographs" />

                     

                    </div>

                     

                    </rich:column>

                     

                    </rich:dataTable>

                    • 7. Re: Using a4j:mediaOutput to display a JPEG from a BLOB
                      ilya_shaikovsky

                      one more time.. if you getting the object from Entity directly successfully and the controller which tries to get it and pass to Stream can't get it - it's a problem of objects interaction design. And responsibility of the component - just call the paint method. If it called - all works fine at component level.

                      • 8. Re: Using a4j:mediaOutput to display a JPEG from a BLOB
                        notify

                        Okay.

                         

                        I can't pass the BLOB = byte [] to the POJO which does the paint method.

                         

                        The BLOB can be displayed as its 'ID', say "[B@1957bfa"

                         

                        But when I try and pass this to paint it the paint method doesn't get called. I can call the paint method when I pass a String, so it must be problem with the byte array.

                         

                        Like you say it must be "a problem of objects interaction design". Will investigate.

                         

                        Thanks for all your help.

                        • 9. Re: Using a4j:mediaOutput to display a JPEG from a BLOB
                          ilya_shaikovsky

                          b.t.w. one side effect which is present with mediaOutput  could cause problem of paint method not get called. If the image for that BLOB was already shown before - browser could believe that it should be get from cache. And yes in that case method will not be called. take a look there http://livedemo.exadel.com/richfaces-demo/richfaces/fileUpload.jsf to check how it solved for FU sample. (look to f:param which adds timestamp to media component every time before reRendering so the URL of image resources differs for requests.) cacheable attribute - controls server side caching only. But that influence only reREnder'ing and if you encountering on the first rendering - should not be the cause.

                           

                          Message was edited by: Ilya Shaikovsky

                          • 10. Re: Using a4j:mediaOutput to display a JPEG from a BLOB
                            notify

                            Unable to get a4j:mediaOutput to pass a byte [] (BLOB from an Entity Bean) to a method in a Seam Bean.

                             

                            I have used;

                            <s:graphicImage value="#{postcards.image}">

                                 <s:transformImageSize width="300" maintainRatio="true"/>

                            </s:graphicImage>

                             

                            Worked perfectly, would have saved days of frustration!

                            • 11. Re: Using a4j:mediaOutput to display a JPEG from a BLOB
                              nbelaevski

                              Roger,

                               

                              Can you please check this article: http://community.jboss.org/docs/DOC-14811 ?

                              • 12. Re: Using a4j:mediaOutput to display a JPEG from a BLOB
                                micnight

                                Hi Nick,

                                 

                                this article helped me a lot to solve my problem (uploading and displaying pictures with Portlet Bridge in GateIn). Thank you.

                                 

                                Regards,

                                Michael