10 Replies Latest reply on Jan 7, 2009 4:05 AM by John Ament

    How to create a view that displays a file.

    John Ament Master

      So I've created a page in my app to allow for file uploads.  It's currently restricted to images.  I have it working so that an entity is created w/ information about the file, and the tmp file that richfaces creates is copied to a path that's stored w/ the entity.


      If I was dealing w/ a plain servlet, it'd be easy enough to just modify the header info, tell it to expect an attachment, give it the name, etc.  How do I do something similar using Seam/JSF?

        • 2. Re: How to create a view that displays a file.
          John Ament Master

          Sort of.  That seems to be able to create an HTML tag (a, img, etc) but i'm looking for something that I can just link to directly.


          instead of creating a page like this:



          <html  xmlns="http://www.w3.org/1999/xhtml"><head><script src="/a4j/g/3_3_0.BETA4/org/ajax4jsf/framework.pack.js" type="text/javascript"></script><script src="/a4j/g/3_3_0.BETA4/org/richfaces/ui.pack.js" type="text/javascript"></script><link class="component" href="/a4j/s/3_3_0.BETA4/org/richfaces/skin.xcss/DATB/eAGbo7-tNnT5DGkAEXoDrg__" rel="stylesheet" type="text/css" /></head><img id="j_id0" src="/a4j/s/3_3_0.BETA4org.ajax4jsf.resource.UserResource/n/s/0/DATA/eAF1VEtrFEEQrmw2uL6jUYMH8YkvtCdKjEpUXONrw8ZANgZUUHpneze9zkxPuns2o6LgwYuviydB8eBVD-pRD3pREAWJ4C8IiIggQtSj1b2bZFx0D8tMT1V9X9X3VT!6Bm1KwlYhK4RWadxdVWUimRKRdBk5qZgcarxsOCn5YaopmF!Hrq8pSOdhoSsZ1axPBJoFWsPSfJXWqOPRoOIMFqvM1b15mMPikGPNMbgCLXnI-KLEy5yVGu9tNepFzL7EIXLZaErEpExdpogr!FAEWJsUNAIdF16JyQKtMXnq7bP9d-69H0hBKg9zXY8qdYL67G8OBS15UEEO8xTmlGwNDSvqLLlwCkxy6vGLtOix3jg08JsQkqgosAQ8phVhHhmmlQGmR0XpSBxiJ4qLwM4BoGUBQCxhZZ00hjbHPXXe3pj81X07ZeM6ZuJmKz28dr3w4!TEPhNhGKw1YlSLQlno5no5P!SK3zun5r9aMmCwzeAy4yth-fpLNc7Gcz6tMFJhAZM4r8sAkYSOM7ZhYmTBOdqRPJ4Y-fxl1aVjFhR7SWlYZrgRLshgpMNIYyCjvoZ2e2xz65KGYTzeB1mn7CtnFjIe1b635uCebTu6dq!5yxb71!2T2rr6vLfNdqsQj3C0Enov1KQf6RxiNMhNnwg51X9858jV3A0rerqIHxsmmmue-4wJNCxJmNAe9aJAm!8LMySETkCIu4fl1MvMHPT3aVjMVQFJ9U270Dh--tm4zaDjGqT1hRCd15NHEMcq55heHBoEAm2LblHOTF9OAmwY89Cd7REuWuIYm2ivNxFp7jl5rjS6s0XDQsnGIqY0Kx3lHrsiobuprQRiYpKJ0gaxYV1Yai25aFbfI0HkJz-GGlqzJ06hJ61viWFDslLSC4ZSfHVi1d039H4rtOQgrfhF3HPjpPG0-cekZmldIVljP-yNESfnfvbjp!yt3wfa63vSnDozPZItKi2pm0y9NtL6YnjyzJOUuV7-IVAcGp06kRFpGhdWChQWQ4XIkPC8InXPJ4Y1MP!nogc7vm8xnEyJ1bZOjIW2m1tC0xKheGmQMorh0wA3zzdXFS4GGUoK1fWav7u578Nza9uMiU6YJ2PED6bNFOPMF!f07O3adc6jEle5GlY0tPWL0SBbM!Dr!wC9JPPY" /></html>



          I just want a url that I can point to like this..



          /a4j/s/3_3_0.BETA4org.ajax4jsf.resource.UserResource/n/s/0/DATA/eAF1VEtrFEEQrmw2uL6jUYMH8YkvtCdKjEpUXONrw8ZANgZUUHpneze9zkxPuns2o6LgwYuviydB8eBVD-pRD3pREAWJ4C8IiIggQtSj1b2bZFx0D8tMT1V9X9X3VT!6Bm1KwlYhK4RWadxdVWUimRKRdBk5qZgcarxsOCn5YaopmF!Hrq8pSOdhoSsZ1axPBJoFWsPSfJXWqOPRoOIMFqvM1b15mMPikGPNMbgCLXnI-KLEy5yVGu9tNepFzL7EIXLZaErEpExdpogr!FAEWJsUNAIdF16JyQKtMXnq7bP9d-69H0hBKg9zXY8qdYL67G8OBS15UEEO8xTmlGwNDSvqLLlwCkxy6vGLtOix3jg08JsQkqgosAQ8phVhHhmmlQGmR0XpSBxiJ4qLwM4BoGUBQCxhZZ00hjbHPXXe3pj81X07ZeM6ZuJmKz28dr3w4!TEPhNhGKw1YlSLQlno5no5P!SK3zun5r9aMmCwzeAy4yth-fpLNc7Gcz6tMFJhAZM4r8sAkYSOM7ZhYmTBOdqRPJ4Y-fxl1aVjFhR7SWlYZrgRLshgpMNIYyCjvoZ2e2xz65KGYTzeB1mn7CtnFjIe1b635uCebTu6dq!5yxb71!2T2rr6vLfNdqsQj3C0Enov1KQf6RxiNMhNnwg51X9858jV3A0rerqIHxsmmmue-4wJNCxJmNAe9aJAm!8LMySETkCIu4fl1MvMHPT3aVjMVQFJ9U270Dh--tm4zaDjGqT1hRCd15NHEMcq55heHBoEAm2LblHOTF9OAmwY89Cd7REuWuIYm2ivNxFp7jl5rjS6s0XDQsnGIqY0Kx3lHrsiobuprQRiYpKJ0gaxYV1Yai25aFbfI0HkJz-GGlqzJ06hJ61viWFDslLSC4ZSfHVi1d039H4rtOQgrfhF3HPjpPG0-cekZmldIVljP-yNESfnfvbjp!yt3wfa63vSnDozPZItKi2pm0y9NtL6YnjyzJOUuV7-IVAcGp06kRFpGhdWChQWQ4XIkPC8InXPJ4Y1MP!nogc7vm8xnEyJ1bZOjIW2m1tC0xKheGmQMorh0wA3zzdXFS4GGUoK1fWav7u578Nza9uMiU6YJ2PED6bNFOPMF!f07O3adc6jEle5GlY0tPWL0SBbM!Dr!wC9JPPY


          • 3. Re: How to create a view that displays a file.
            John Ament Master

            Sorry for scrolling.


            I should have made it clear - how do i make user avatars like this site uses?

            • 4. Re: How to create a view that displays a file.
              Ronald van Kuijk Apprentice

              The resources are provided via a servlet. That is the 'jsf' way of doing it and also the seam way. What you could do is create a small jsf component that creates this url for you (that is what e.g. the script and style tags in richfaces do)

              • 5. Re: How to create a view that displays a file.
                Ingo Jobling Master

                You can find the full source code for this website in the Seam package in the directory /examples/wiki.

                • 6. Re: How to create a view that displays a file.
                  John Ament Master

                  I think I'm asking for the wrong thing.


                  I think all I need is some use of resteasy to accomplish this.

                  • 8. Re: How to create a view that displays a file.
                    John Ament Master

                    that looks fine too, but I ended up doing it using resteasy.  Not bad to be honest.



                    @Name("viewImage")
                    @Path("/viewimage")
                    public class ViewImage implements java.io.Serializable{
                            @In(create=true) StoredFileDAO    storedFileDAO;
                            @In(create=true) UserDAO                  userDAO;
                            @In(create=true) ConfigSettingDAO configSettingDAO;
                            @In(create=true) AvatarDAO        avatarDAO;
                            
                            @GET
                            @Path("/{username}/{filename}")
                            @ProduceMime("image/jpeg")
                            public byte[] viewImage(@PathParam("username")String username
                                            ,@PathParam("filename")String filename) {
                                    User u = userDAO.findUserByUsername(username);
                                    if(u == null) return defaultImage();
                                    StoredFile storedFile = storedFileDAO.getByUserAndName(u, filename);
                                    if(storedFile == null) return defaultImage();
                                    ConfigSetting setting = configSettingDAO.get("NFS_BASE_MOUNT");
                                    String value = setting.getSettingValue();
                                    String userDir = value + "/" +storedFile.getUser().getUsername();
                                    File f = new File(userDir+"/"+storedFile.getName());
                                    int size = storedFile.getSize();
                                    if(f.exists()) {
                                            try {
                                                    FileInputStream fis = new FileInputStream(f);
                                                    byte[] b = new byte[size];
                                                    fis.read(b);
                                                    return b;
                                            } catch (IOException e) {
                                                    return defaultImage();
                                            }
                                    } else { 
                                            return defaultImage();
                                    }
                            }
                    }



                    defaultImage() returns a simple 404 image not found type of graphic.  I didn't see any indication, and I wanted to avoid crashing my system, so I didn't try to see what happens if this returns a java.io.File rather than a byte[], I would kind of prefer a File though. as it seems unnecessary to handle the IOException on a file that was written there using this app and therefore must be owned by this app.

                    • 9. Re: How to create a view that displays a file.
                      Ingo Jobling Master

                      Excellent!  Thanks for posting your solution.


                      These annotations are for restEasy?


                              @GET
                              @Path("/{username}/{filename}")
                              @ProduceMime("image/jpeg")
                      


                      • 10. Re: How to create a view that displays a file.
                        John Ament Master

                        well, from jax-rs.  it's based on the examples in the seam docs for resteasy.