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

    File download from java server to flex client


      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.




          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.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
                  private function onProgress(event:ProgressEvent):void{
                      //handle the progress
                  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();
          OutputStream os = response.getPortletOutputStream();




          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;         
                            //Suppousing you want to send a pdf file that is on filesystem..
                            File file = new File("THE PATH TO YOUR FILE");
                            InputStream in =
              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..
                            servletOutputStream = resp.getOutputStream();
                            servletOutputStream.write(bytes, 0, bytes.length);

                        } catch (Exception e) {
                              ErrorStr = "Error Streaming the Data";
                        } finally {
              servletOutputStream != null ) {

                              if( bis != null ) {

                              if( bos != null ) {





              Hope this be of better help.


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


                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.




                  hope this helps,

                  (and if it solved it mark it as such)


                  kind regards.



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

                    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");



                    • 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.