lunedì 9 luglio 2012

Oracle UCM: Download Documents from UCM to your PC using RIDC

In this post I provide another functionality to download files from Content Server to your PC using RIDC.

Please note that Perform Documents Check-In using Remote IntraDoC (RIDC)  contains the prerequisite code since in this post I don't provide the full java code but just additions.

This is a simple code snippet that downloads a single file from Oracle UCM to your PC.

Add a new method to our already discussed UCMAdapter.java

public InputStream getFile(String username, String documentId) {
        ServiceResponse severiceResponse = null;
       
        try {
            IdcClient client = getIdcClient();
            DataBinder dataBinderReq = client.createBinder();
            dataBinderReq.putLocal("IdcService", "GET_FILE");
            dataBinderReq.putLocal("dID", documentId);

            severiceResponse = client.sendRequest(new IdcContext(username), dataBinderReq);
            InputStream is = severiceResponse.getResponseStream();
            System.out.println("GET_FILE size: " + is.available()); 
            return is;           
                               
        } catch(Exception ex) {
            System.out.println("Error GetFile(): " + ex.getMessage());               
        }
        return null;
}//-

Then recreate an empty file Main.java and paste this code:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

public class Main {
   
    public static void main(String[] args) {
       
    //EDIT THESE VARIABLES
        //ID of the file to retrieve
        String docID="6231";
        //Where to download file
        String fileSaveLocation = "C:\\file.pdf";
        //User who performs file download
        String username = "weblogic";
    //   

        //Instanciate objects
         UCMAdapter ucm = new UCMAdapter();
         new Document();
        
         //Download file
         try
           {
           File f=new File(fileSaveLocation);
           InputStream inputStream= ucm.getFile(username, docID);
           OutputStream out=new FileOutputStream(f);
           byte buf[]=new byte[1024];
           int len;
           while((len=inputStream.read(buf))>0)
           out.write(buf,0,len);
           out.close();
           inputStream.close();
           System.out.println("\n File correctly created.");
           }
           catch (IOException e){
          System.out.println("\n Error creating file.");
          }
         
    }//
}//end class

Note that in the code above you need to set some variables.

docID = "6231"

Is the Document ID of the document we want to download to our PC. You can modify this code in a more elegant way which performs a document search and automatically retrieves Document ID of the file you need to download.

fileSaveLocation = "C:\\file.pdf"

Is the path where you download the file and which kind of file extension you are retrieving...this is a really basic code, you may need to introduce a more sophisticated feature like perform a check of file extension directly from Content Server and automatically set this variable using the appropriate file name and extension based on file extension readed from UCM.

Finally

username = "weblogic"

Is the username who performs file dowload.

That's all!!

3 commenti:

  1. Hi,
    Thanks for the code.
    I'm facing one issue here,while trying to download the file,blank file is egtting downloaded from the content server .There is absolutely no error in the logs.I have tried a lot of times but it does not seem to work for me.

    RispondiElimina
  2. Well, we got the same thing. Turns out, that the IOException on the audit logs of server is not propagated to the RIDC client. (1.11.1 btw 1.11.9 contains 1.11.1 code!)

    RispondiElimina
  3. Hi, Thanks Thanks Thanks..... for the code.

    RispondiElimina