5 Replies Latest reply on Aug 7, 2009 8:24 AM by Leo van den berg

    load an image from remote source

    Sathish Kumar Newbie

      Hi
      Good Day to Everyone...
      Here with i have one doubt regards that load an image from physical path(local or remote drive).


      suppose i'm using img src-path image placed within the context means no problem if i refered E:/images/123.jpg means i'm getting problem(can't load/show the image)


      suppose i'm using h:graphicImage height-'90' width-'105'  url-file://///192.168.1.11/E$/images/123.jpg i can't see the image


      suppose if i'm using s:graphicImage how it's possible?



        • 1. Re: load an image from remote source
          Leo van den berg Master

          Hi Satish,


          You can use additional data sources for the s-tag. I use the stream and I've added a stream reader to a specific bean. Your stream reader can be based on whatever URL (with http for webbased and file for local access). I've included a basic example
               


          public InputStream serveContent(String content){
                    InputStream s = null;
                    try {
                         URL url = new URL(content);
                         s = url.openStream();
                    } catch (MalformedURLException e) {
                         System.out.println(e.getMessage());
                    } catch (IOException e) {
                         System.out.println(e.getMessage());
                    }
                    return s;
               }
               



          I call the function with:


          Another possiility is to use the byte-array as source, which can be provided by your entity-bean. I use the combination of both in a table with icons and a tooltip. The icon is automatically generated and saved in the database together with the fullname of a file where I store the full-size image. Whenever I hoover over the image a tooltip shows the full-size image.


          hopefully this helps.


          Leo



          • 2. Re: load an image from remote source
            Andrew Lee Newbie

            I had met a similar problem as yours. The client need to submit a excel file to the server. To implement, use a seam:fileUpload and create a byte array to accept its data, then format the byte data as excel file at the server side.
            I think properly you format the byte data as jpeg file.

            • 3. Re: load an image from remote source
              Sathish Kumar Newbie

              Click HELP for text formatting instructions. Then edit this text and check the preview.
              Hi Mr. Leo van den Berg
              Thanks a lot it's running when i use URL class
              But i tried to run using output stream i getting the error


              error is :


              Caused by: java.lang.IllegalStateException: Servlet response already use Writer, OutputStream not possible





              public byte[] getShowImage(){ 
              
              
                        int length = 0;
                        byte[] bytes = null;     
                      FileInputStream in = null;
                        try {
                             in = new FileInputStream("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/Sunset.jpg");
                             length = in.available();
                             bytes = new byte[length];
                           in.read(bytes);
                           in.close();
                        } catch (FileNotFoundException e) {          
                             e.printStackTrace();
                        }catch (IOException e) {
                             e.printStackTrace();
                        }
                      FacesContext context = FacesContext.getCurrentInstance();
                      HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
                      try {
                             response.setContentType("image/jpg");
                             response.setContentLength(length);
                             response.getOutputStream().write(bytes);
                             context.responseComplete();
                        } catch (IOException e) {
                             e.printStackTrace();
                        }
                      
                        return bytes;
                   }



              • 4. Re: load an image from remote source
                Sathish Kumar Newbie

                Hi Mr. Leo van den Berg


                Thanks a lot it's running when i use URL class But i tried to run using output stream i getting the error




                error is :


                Caused by: java.lang.IllegalStateException: Servlet response already use Writer, OutputStream not possible





                public byte[] getShowImage(){ 
                
                
                          int length = 0;
                          byte[] bytes = null;     
                        FileInputStream in = null;
                          try {
                               in = new FileInputStream("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/Sunset.jpg");
                               length = in.available();
                               bytes = new byte[length];
                             in.read(bytes);
                             in.close();
                          } catch (FileNotFoundException e) {          
                               e.printStackTrace();
                          }catch (IOException e) {
                               e.printStackTrace();
                          }
                        FacesContext context = FacesContext.getCurrentInstance();
                        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
                        try {
                               response.setContentType("image/jpg");
                               response.setContentLength(length);
                               response.getOutputStream().write(bytes);
                               context.responseComplete();
                          } catch (IOException e) {
                               e.printStackTrace();
                          }
                        
                          return bytes;
                     }



                • 5. Re: load an image from remote source
                  Leo van den berg Master

                  Hi Satish,


                  If it is your intention to load some binary resource and serve it to the user browser, you could use a bean which extends Abstractresource. Another option is to use the build in component DocumentStore.


                  Leo