8 Replies Latest reply on Aug 3, 2011 3:04 AM by edwinrosenino

    File download from java server to flex client

    ahobbsy10

      Does anyone know any examples?

        • 1. Re: File download from java server to flex client
          P.o.l.a.c.o Level 1

          I used something similar to this code to handle the download of a dinamically generated pdf file.

           

          /////////

          Flex:

          public class Downloader
              {
                 
                  private var service:HTTPService = null;
                  private var urlLoader:URLLoader = null;       
                 
                 
                  public function Downloader()
                  {
                      service = //an HTTP Service pointing to your app;
                      urlLoader = new URLLoader();
                     urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
                     
                  }

                    //sends the file download request.
                  public function getFile():void{
                     
                     
                      var variables:URLVariables = new URLVariables();
                     //add your params to the variables

                    //variables.XXX = SSS;
                     
                      var req:URLRequest = new URLRequest(service.url);
                      req.method = URLRequestMethod.POST;
                      req.data = variables;
                      urlLoader.load(req);
                     
                     
                      urlLoader.addEventListener(Event.COMPLETE, onResult);

           

                      urlLoader.addEventListener(Event.OPEN, onOpen);

           

                      urlLoader.addEventListener(ProgressEvent.PROGRESS, onProgress);

           

                      urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onFault);
                      urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onFault);
                     
                         
                  }
                 
                  private function onOpen(event:Event):void{
                      //handle the download start
                      //urlLoader.bytesLoaded;
                      //urlLoader.bytesTotal;
                  }
                 
                  private function onProgress(event:ProgressEvent):void{
                      //handle the progress
                      //event.bytesLoaded;
                      //event.bytesTotal;
                  }
                 
                  public function onResult(event:Event):void{
                      //handle the complete
                      var aPDFFile:Object = urlLoader.data;
                      var fr:FileReference = new FileReference();
                      fr.save(data.pdfCatalog, "thefileName.extension");
                  }
                 
                 
                  public function onFault(event:Event):void{
                      //handle the fault.
                  }

           

              }

          ///////////////////

           

          Java side:

          //////

          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          //add the dynamically generated file data to the ByteArrayOutputstream

          byte[] ba = baos.toByteArray();
          response.setContentType("application/pdf");
          OutputStream os = response.getPortletOutputStream();
          os.write(ba);
          os.flush();
          os.close();

          /////

           

           

          hope it helps.

          1 person found this helpful
          • 2. Re: File download from java server to flex client
            ahobbsy10 Level 1

            Thanks for your reply but i am having trouble understanding your java code could you post the whole thing please?

            • 3. Re: File download from java server to flex client
              P.o.l.a.c.o Level 1

              Sorry I included a demo that I was using for a portlet here is a quick example that uses a servlet:

              (I have done this code with the forums editor so it could have some errors)

               

              Java side:

              //////

               

              private void streamBinaryData(HttpServletResponse resp)
                {
                      String ErrorStr = null;
                     

                      ServletOutputStream servletOutputStream = null;
                     
                     
                       

                       
                       
                        BufferedInputStream bis = null;
                        //we will read the bytes of the file here
                       
              ByteArrayOutputStream bos = null;         
                        try{
                            //Suppousing you want to send a pdf file that is on filesystem..
                            File file = new File("THE PATH TO YOUR FILE");
                            InputStream in =
              file.newInputStream();
                          
              bis = new BufferedInputStream(in);
                            bos = new ByteArrayOutputStream();
                            
                            byte[] buff = new byte[1024];
                            int bytesRead;
                            // Simple read/write loop.
                            while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                              bos.write(buff, 0, bytesRead);
                            }
                           
              byte[] bytes = bos.toByteArray();
                            //suppousing you are returning a pdf..
                            resp.setContentType("application/pdf");
                            resp.setContentLength(bytes.length);
                           
                            servletOutputStream = resp.getOutputStream();
                            servletOutputStream.write(bytes, 0, bytes.length);
                           
                           

                        } catch (Exception e) {
                              e.printStackTrace();
                              ErrorStr = "Error Streaming the Data";
                              outstr.print(ErrorStr);
                        } finally {
                             
                              if(
              servletOutputStream != null ) {
                                 servletOutputStream.flush();
                                 servletOutputStream.close();
                              }
                             

                              if( bis != null ) {
                                bis.close();
                              }

                              if( bos != null ) {
                                bos.close();
                              }

                        } 
                     
                }

               

              /////

               

              Hope this be of better help.

              regards.

              • 4. Re: File download from java server to flex client
                damu1234

                Sir

                I upload image file to database using java and flex.Now i want to display the image in flex RIA  from database.How can i do that?

                Please Help

                • 5. Re: File download from java server to flex client
                  P.o.l.a.c.o Level 1

                  I guess you could take a look at this example about how to build an image from an array of bytes.

                   

                  http://onflash.org/ted/2007/12/progressive-image-loading-with.php

                   

                  hope this helps,

                  (and if it solved it mark it as such)

                   

                  kind regards.

                   

                  Polaco.

                  • 6. Re: File download from java server to flex client
                    edwinrosenino

                    P.o.l.a.c.o,  can you show the part FileReference.download(url, name) in flex code, to download file that you get from java server? and where should data.pdfCatalog below come from?

                     

                    var fr:FileReference = new FileReference();
                    fr.save(data.pdfCatalog, "thefileName.extension");

                     

                    thx

                    • 7. Re: File download from java server to flex client
                      P.o.l.a.c.o Level 1

                      Hi Edwin,

                       

                      As you may notice this thread is quite old, I think would be kind difficult to find the file since I no longer work for the company I did the project and also because the pc where I got some tests is out of order. Anyway I will search for it on my mails when I have some time and let you know if I find something.

                       

                      regards,

                       

                      Polaco.