cancel
Showing results for 
Search instead for 
Did you mean: 

FTP to Alfresco FTP transfer

patil
Champ on-the-rise
Champ on-the-rise
Hi ,

I have to transfer the files from FTP to alfresco. For this i am using FTP to FTP(Alfresco) transfer. I am successful in doing this.

But while moving the content, I should be able to create the contents of custom Content types and assign the metadata.
How to achieve this?



I am using the below code for the  FTP to FTP trasfer(IT makes cm:content type transfer)

   FTPUtilityJobs.checkFTPConnection(alfrescoClient, alfrescoFtpIP, Integer.valueOf(alfrescoFtpPort));
      FTPUtilityJobs.checkFTPConnection(client, ftpIP, Integer.valueOf(ftpPort));

      ftpLogin = client.login(ftpUserName, ftpPassword);
      LOGGER.debug("logged in to the ftp:::" + ftpLogin);
      LOGGER.debug("Parameters of the userna");
      alfrescoLogin = alfrescoClient.login(alfrescoFtpUserName,
            alfrescoFtpUserpassword);
      LOGGER.debug("logged in to the Alfresco:::" + alfrescoLogin);

      if (ftpLogin && alfrescoLogin) {
         LOGGER.debug("Connected to both the ftps");

         
         //create the initially required folder structure
         FTPUtilityJobs.createInitialFolderStructure(alfrescoClient ,alfrescoFtpDestinationPath);
         alfrescoClient.changeToParentDirectory();         
         FTPUtilityJobs.setInitialChangeWorkingDirectory(alfrescoFtpDestinationPath, alfrescoClient);
          LOGGER.debug("Working directory is::"+alfrescoClient.printWorkingDirectory());
            
         List<String> destFilesAndDirectory = FTPUtilityJobs.getRecursiveFileListingWithChangeDirectory(alfrescoClient,alfrescoFtpDestinationPath);
         
         
         
         
         //read all the files from the ftp from where the files to be migrated
         Map<String,FTPFile> foldersAndDirectory = FTPUtilityJobs.getRecursiveFileListingMap(client,ftpSourcePath);
         Set<String> set = foldersAndDirectory.keySet();            
         Iterator<String> iterator =set.iterator();
         List<String> sortePaths = new ArrayList<String>();
         while(iterator.hasNext()){
            sortePaths.add(iterator.next());
         }
         //sorted so that folder creation paths will be proper
         Collections.sort(sortePaths);
         
         //get the alfresco structure to decide whether to copy or not
         //Map<String,FTPFile> alfrescoFoldersAndDirectory = FTPUtilityJobs.getRecursiveFileListingMap(alfrescoClient,alfrescoFtpDestinationPath);
         
            // now create alll the folders         
         createRequiredFolderStructure(sortePaths, alfrescoClient, foldersAndDirectory, alfrescoFtpDestinationPath, ftpSourcePath);
         
         
      
         
         
            //Now start moving the files
            for(String locationKey :sortePaths){         
               
               if(!destFilesAndDirectory.contains(alfrescoFtpDestinationPath+(locationKey.substring(ftpSourcePath.length())))){
                  
               
               int attempt = 0;
      __reattempt:               
               try{
               //String locationKey = iterator.next();
               FTPFile file = foldersAndDirectory.get(locationKey);         
               if (!file.isDirectory()) {
                  // Let's just assume success for now.
                  alfrescoClient.enterRemotePassiveMode();
                  client.enterRemoteActiveMode(InetAddress
                        .getByName(alfrescoClient.getPassiveHost()),
                        alfrescoClient.getPassivePort());
                  LOGGER.debug("file transfer started");

                  // Although you would think the store command should be
                  // sent to server2
                  // first, in reality, ftp servers like wu-ftpd start
                  // accepting data
                  // connections right after entering passive mode.
                  // Additionally, they
                  // don't even send the positive preliminary reply until
                  // after the
                  // transfer is completed (in the case of passive mode
                  // transfers).
                  // Therefore, calling store first would hang waiting for
                  // a preliminary
                  // reply.

                  if (client.remoteRetrieve(locationKey)) {
                     LOGGER.debug("retrieving from FTP servers is successful");                                             
                     boolean uploaded = alfrescoClient.remoteStore(alfrescoFtpDestinationPath+(locationKey.substring(ftpSourcePath.length())));
                     if(uploaded || attempt>=2){
                        client.completePendingCommand();
                        alfrescoClient.completePendingCommand();
                        LOGGER.debug("retrieving and creating from FTP servers is successful");   
                     }else{
                        attempt++;
                         break __reattempt;
                     }
                     
                  
                  }

               } /*else if (file.isDirectory()) {
                  
                  
                  LOGGER.debug("Creatring the directory::"
                        + file.getName());
                  // Creates a new subdirectory on the FTP server in the
                  // current directory (if a relative pathname is given)
                  // or where specified (if an absolute pathname is
                  // given).
                  
                  
                  if (alfrescoClient.makeDirectory(alfrescoFtpDestinationPath+(locationKey.substring(ftpSourcePath.length())))) {
                     
                     LOGGER.debug("Folder structure created in the path:::"+alfrescoFtpDestinationPath+(locationKey.substring(ftpSourcePath.length())));
                  } else {
                     
                           LOGGER.debug("Could not create the folderin alfresco"+alfrescoFtpDestinationPath+(locationKey.substring(ftpSourcePath.length())));
                  }

               }*/
               }catch (Exception e) {
                  LOGGER.error("Error happened in connecting to the alfresco FTP serer");      
                  throw new WebScriptException("Exception happened while transferring the files");
               }
               
               }else{
                  LOGGER.debug("Already the file exists now dont copy the content");
               }

            
               
            }

      }
2 REPLIES 2

alinak
Champ in-the-making
Champ in-the-making
I don't know how to achieve but thanks for sharing code i will use your code & try to solve your problem.

patil
Champ on-the-rise
Champ on-the-rise
I was able to do this task.

Thanks,
Patil
Cignex Technologies
Bangalore