11 Replies Latest reply: May 31, 2012 4:41 PM by 868989 RSS

    Download a static excel file in ADF

    868989
      Hi,
      How to download an excel file whose content is static in ADF. If I use af:fileDownloadActionListener I need to read the data from the static file and write it to the excel file. As the content is static I don't want to read and write the outputStream of the file.

      Is there any way I can directly download a static file in ADF.

      Thanks,
      Sree
        • 1. Re: Download a static excel file in ADF
          raphael rodrigues - oracle
          Have you tried this:

          http://saumoinak.blogspot.com.br/2011/04/file-download-in-adf.html

          Regards,
          • 2. Re: Download a static excel file in ADF
            868989
            Hi,
            Thanks for the reply. I don't want to read the static file and write its content to the downloadable file. I just want to download the file that presents on the server/ or in my source control system.

            Thanks
            • 3. Re: Download a static excel file in ADF
              raphael rodrigues - oracle
              You could accomplish this, just customizing the code was showed above a little:

              public void download(FacesContext context,
              OutputStream out) throws IOException {
              File f =
              new File("PUTHERETHEPATHOFYOURFILE");
              FileInputStream fis;
              byte[] b;
              try {
              fis = new FileInputStream(f);

              int n;
              while ((n = fis.available()) > 0) {
              b = new byte[n];
              int result = fis.read(b);
              out.write(b, 0, b.length);
              if (result == -1)
              break;
              }
              } catch (IOException e) {
              e.printStackTrace();
              }
              out.flush();
              }
              • 4. Re: Download a static excel file in ADF
                Navaneetha Krishnan Nataraj
                Have a golink component and have the destination property point to the specified location.

                Thanks,
                Navaneeth
                • 5. Re: Download a static excel file in ADF
                  868989
                  Hi,
                  As I have excel file, I need to use apache POI apis to read the excel / write the excel. I am finding out a way to directly download the static excel file.

                  Thanks
                  • 6. Re: Download a static excel file in ADF
                    868989
                    Navaneeth,
                    I think, we should not use goLink as its purpose to use is to navigate to the external application.
                    • 7. Re: Download a static excel file in ADF
                      raphael rodrigues - oracle
                      For parser your excel file, you have two java api options: apache poi and jexcel api. Depends on your excel complexity file, you also could be consider transform in csv file extension and parser with a normal text java file....


                      Regards,
                      • 8. Re: Download a static excel file in ADF
                        umesh.agarwal
                        You can use the following code to download the file.
                            public void downloadFileAction(FacesContext fctx, OutputStream out) {
                                String fileContentType = null;
                                String fileName = null;
                                BlobDomain fileData = null;
                                Long fileId = null;
                                Map<String, Object> pageFlow =
                                    AdfFacesContext.getCurrentInstance().getPageFlowScope();
                                if (pageFlow != null) {
                                    fileId = (Long)pageFlow.get("reviewFileID");
                                }
                                if (fileId != null) {
                                    DCBindingContainer bindings = ADFUtils.getDCBindingContainer();
                                    //Assuming your file data is stored in a db table. FileAttachmentTransVO is the view object built on top of this table.
                                    ViewObjectImpl fileAttach =
                                        (FileAttachmentTransVOImpl)bindings.findIteratorBinding("FileAttachmentTransVO1Iterator").getViewObject();
                                    RowQualifier qualf = new RowQualifier(" FileId = " + fileId);
                                    Row[] rows = fileAttach.getFilteredRows(qualf);
                                    if (rows.length == 1) {
                                        fileName = (String)rows[0].getAttribute("FileName");
                                        fileContentType =
                                                (String)rows[0].getAttribute("FileContentType");
                                        fileData = (BlobDomain)rows[0].getAttribute("FileData");
                                    }
                                }
                                Long length = fileData.getLength();
                                HttpServletResponse response =
                                    (HttpServletResponse)fctx.getExternalContext().getResponse();
                                response.setHeader("Content-Disposition",
                                                   "attachment;filename=\"" + fileName + "\"");
                                response.setContentType(fileContentType != null ? fileContentType :
                                                        "application/x-download");
                                response.setContentLength(length.intValue());
                        
                                try {
                                    InputStream in = fileData.getBinaryStream();
                                    byte[] buf = new byte[1024];
                                    int count;
                                    while ((count = in.read(buf)) >= 0) {
                                        out.write(buf, 0, count);
                                    }
                                    in.close();
                                    out.flush();
                                    out.close();
                                    fctx.responseComplete();
                                } catch (IOException ex) {
                                    ex.printStackTrace();
                                }
                            }
                        • 9. Re: Download a static excel file in ADF
                          Navaneetha Krishnan Nataraj
                          Can you access the file present in the server/ or in my source control system thru HTTP request?
                          If yes, you could use goLink component- that will have the destination property set with the http path.

                          Sample:
                                  <af:goLink text="Download Excel Sheet" id="gl1"
                                             destination="http://adfsampleapplications.googlecode.com/svn/trunk/PS5FileDownloadSample"/>
                          If the file is part of your application itself, you could use as in the sample
                          http://adfsampleapplications.googlecode.com/svn/trunk/PS5FileDownloadSample.rar

                          Thanks,
                          Navaneeth
                          • 10. Re: Download a static excel file in ADF
                            Arunkumar Ramamoorthy-Oracle
                            I think, we should not use goLink as its purpose to use is to navigate to the external application.
                            No. Not necessarily.
                            • 11. Re: Download a static excel file in ADF
                              868989
                              Yes, we can download the file a file using af:goLink but if we use af:goLink, user will be out of current application context. So, I used apache POI apis and af:fileDownloadActionListener to download the file.

                              Thanks,
                              Sree