Monday 6 April 2015

ADF: How to download a file from database in ADF Web Application

This post explains how to download a file stored in the database in a Blob/Clob column from a ADF Web Application.
The data from the table will be displayed in a table format and one column will have a image icon to download the corresponding file.

The table structure used in this sample application :
 CREATE TABLE IMAGE_STORE  
  (  
   IMAGE_ID NUMBER ,  
   FILE_NAME VARCHAR2(200 BYTE) ,  
   IMAGE BLOB,  
   MIME_TYPE VARCHAR2(100 BYTE),  
   CONSTRAINT IMAGE_STORE_PK PRIMARY KEY (IMAGE_ID)  
  );  
Sample code to insert a blob into table is available : Johny's Tips: PL/SQL: Sample code to insert BLOB into Oracle database table.
  • Create the Model Project and create the ViewObject and add that to the Application Module.
  • Create a page and add the DataControl as a table to the project.
  • Create a Attribute Binding for the Clob/Blob object. 
    [Thanks to Cvele_new_account from OTN Forum]

  • Add a link component as one column and add a fileDownloadActionListener.
             Note:- If you don't specify the fileName, the file will be written to the browser.
  • Create a Managed Bean and write the code as below for Blob Object.
   public void downloadBlobFile(FacesContext facesContext, OutputStream outputStream)  
   {  
     BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();  
     AttributeBinding attr = (AttributeBinding) bindings.getControlBinding("Image");  
     if (attr != null)  
     {  
       BlobDomain blob = (BlobDomain) attr.getInputValue();  
       try  
       {  // copy the data from the blobDomain to the output stream   
         IOUtils.copy(blob.getInputStream(), outputStream);  
         blob.closeInputStream();  
         outputStream.flush();  
       }  
       catch (IOException e)  
       {  
         // handle errors  
         e.printStackTrace();  
         FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), "");  
         FacesContext.getCurrentInstance().addMessage(null, msg);  
       }  
     }  
   }  
  • Create a Managed Bean and write the code as below for Clob Object.
   public void downloadClobFile(FacesContext facesContext, OutputStream outputStream)  
   {  
     BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();  
     AttributeBinding attr = (AttributeBinding) bindings.getControlBinding("Image");  
     if (attr != null)  
     {  
       ClobDomain clob = (ClobDomain) attr.getInputValue();  
       try  
       {  // copy the data from the clobDomain to the output stream   
         IOUtils.copy(clob.getCharacterStream(), outputStream);  
         clob.closeInputStream();  
         outputStream.flush();  
       }  
       catch (IOException e)  
       {  
         // handle errors  
         e.printStackTrace();  
         FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), "");  
         FacesContext.getCurrentInstance().addMessage(null, msg);  
       }  
     }  
   }  
Note:- IOUtils (org.apache.commons.io.IOUtils) is a class from the Apache Commons IO Package (http://www.apache.org/). Please download it directly from http://projects.apache.org/projects/commons_io.html


Sample application built using Jdev 12.1.3 can be downloaded from here.

Reference: Timo Hahn's: JDev11.1.2.1.0: Handling images/files in ADF (Part 2)

Feel free to point out if anything is missing/wrong in this blog.

5 comments:

  1. Replies
    1. Thanks Ashish. Getting inspired by guys like you :)

      Delete
  2. Hi,
    Can you re-upload the application sample. The download link says its in your bin and not available for downloading.
    Thank you.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete