Thursday, September 15, 2011

Download File in Oracle ADF GUI : -af:fileDownloadActionListener and getRealPath() and weblogic.xml and show-archived-real-path-enabled

 

I had simple requirement to allow the user to download file from ADF Application i.e. PDF file , Word file.

So I am creating simple application with commandbutton with filedownloadactionlistener.

Please download the application Download Here

I added testdownload.txt file inside ViewController\public_html folder

Command Button

image

I added following code to dwdFile method.

FileDownloadActionListener Method  

    public void dwdFile(FacesContext facesContext, OutputStream out) {
        FacesContext fx = FacesContext.getCurrentInstance();
        ServletContext servCtx =
            (ServletContext)fx.getExternalContext().getContext();
        System.out.println(servCtx.getRealPath("/"));
        File file = new File(servCtx.getRealPath("/") + "testdownload.txt");
        FileInputStream fdwd;
        byte[] bt;
        try {
            System.out.println(file.getCanonicalPath());
            System.out.println(file.getAbsolutePath());
            fdwd = new FileInputStream(file);
            int checkline;
            while ((checkline = fdwd.available()) > 0) {
                bt = new byte[checkline];
                int rst = fdwd.read(bt);
                out.write(bt, 0, bt.length);
                if (rst == -1)
                    break;
            }
            out.flush();
        } catch (IOException e) {
            this.addMessage(FacesMessage.SEVERITY_ERROR,
                            "File cannot be downloaded , Please contact administrator.");
        }
    }
 
When I ran the jspx locally it downloaded the file successfully.
 

Deploying application as EAR

 

When I deployed application as ear and click the download button it did not downloaded the file and it printed null for getRealPath().

 

After some time I figured out that I have to generate weblogic.xml

 

image

 

and set the following property to true.

Enable getRealPath() results for archived web apps.

 

image

 

image



 

Now when I deployed the application on EAR ,it allowed me to download the file successfully.


Reason


There is known CR with Weblogic for this Issue


CR299135 -  Weblogic getRealPath Issue

9 comments:

  1. This works fine...But when I use to download files from database having BLOG type column then I am getting error after first time download of file...means first time i was able to download linked file ..but when i click again on same link then i am getting Java Script is not enabled message in downloaded empty file... same issue i put on oracle forum also..

    ReplyDelete
    Replies
    1. have you got remedy/solution for your issue.. Same issue im facing right now..

      Delete
  2. Replies
    1. Thanks , Share Knowledge and Gain Knowledge.

      Delete
  3. Hi Hasim,
    I want to download a .csv file using "fileDownloadActionListener". My requirment is to download a file from WLS to the machine that runs the application. To read an write I use OpenCSV library.
    I referred your application in order to get an idea. Could you please let me know where/how did you give a value to "Dwdfile.dwdFile(FacesContext facesContext, OutputStream out)" method's "out" attribute when it is calling in the "dwdfile.jspx"?

    Regards !
    Sameera

    ReplyDelete
    Replies
    1. When you use filedownloadactinolistener then binded method should have this two parameter (FacesContext context, OutputStream out). Hope this answers the question. Also, When you download the file ,It is downloaded at application end and you need to provide path based on place where you put the file.

      Delete
  4. Thanks Hasim Nice post .How to get the file from UCM and download it

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Hi Hasim,
    It's a nice post, could you explain me how to convert an ADF UI page to a pdf document?

    ReplyDelete