| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 
 | package com.auscult.ftp;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
public class FTPFunctions implements Job {
 
private String host;
private String username;
private String password;
private int port;
private static String remoteFilePath;
private static String savePath;
private static String parentDir;
private static String currentDir;
private static String saveDir;
private static FTPClient ftpClient = new FTPClient();
 
 public FTPFunctions(String server, int pPort, String pUsername, String pPassword) throws Exception {
 	host = server;
 	port = pPort;
 	username = pUsername;
 	password = pPassword;
 	ftpClient.addProtocolCommandListener (new PrintCommandListener(new PrintWriter(System.out)));
        int reply;
        ftpClient.connect( host , port);
        System.out.println("FTP URL is:"+ftpClient.getDefaultPort());
        reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            throw new Exception("Exception in connecting to FTP Server" );
        }
        ftpClient.login(username, password);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();     
 
 }
 
 public static boolean downloadFTPFile(FTPClient ftpClient, String RemoteFilePath, String SavePath) throws IOException {
 	remoteFilePath  = RemoteFilePath;
 	savePath    = SavePath;
 	File downloadFile = new File(savePath);
 
     File parentDir = downloadFile.getParentFile();
     if (!parentDir.exists()) {
         parentDir.mkdir();
     }
 
     OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
 
     try {
 
         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
         return ftpClient.retrieveFile(remoteFilePath, outputStream);
     } catch (IOException ex) {
         throw ex;
     } finally {
         if (outputStream != null) {
             outputStream.close();
         }
     }
    }
 
 public static void downloadDirectory(FTPClient ftpClient, String ParentDir, String CurrentDir, String SaveDir) throws IOException {
 	parentDir  = ParentDir;
 	currentDir = CurrentDir;
 	saveDir    = SaveDir;
     String dirToList = parentDir;
     if (!currentDir.equals("" )) {
         dirToList += "/" + currentDir;
     }
     FTPFile[] subFiles = ftpClient.listFiles(dirToList);
     if (subFiles != null && subFiles.length > 0) {
         for (FTPFile aFile : subFiles) {
             String currentFileName = aFile.getName();
             if (currentFileName.equals("." ) || currentFileName.equals(".." )) {
                 continue;
             }
             String filePath = parentDir + "/" + currentDir + "/"
                     + currentFileName;
             if (currentDir.equals("" )) {
                 filePath = parentDir + "/" + currentFileName;
             }
             String newDirPath = saveDir + parentDir + File.separator + currentDir + File.separator + currentFileName;
             if (currentDir.equals("" )) {
                 newDirPath = saveDir + parentDir + File.separator
                           + currentFileName;
             }
             if (aFile.isDirectory()) {
                 File newDir = new File(newDirPath);
                 boolean created = newDir.mkdirs();
                 if (created) {
                     System.out.println("CREATED the directory: " + newDirPath);
                 } else {
                     System.out.println("COULD NOT create the directory: " + newDirPath);
                 }
                 downloadDirectory(ftpClient, dirToList, currentFileName, saveDir);
             } else {
                 boolean success = downloadFTPFile(ftpClient, filePath, newDirPath);
                 if (success) {
                     System.out.println("DOWNLOADED the file: " + filePath);
                 } else {
                     System.out.println("COULD NOT download the file: " + filePath);
                 }
             }
         }
     }
 }
 
 public void disconnect(){
        if (FTPFunctions.ftpClient.isConnected()) {
             try {
                 FTPFunctions.ftpClient.logout();
                 FTPFunctions.ftpClient.disconnect();
             } catch (IOException f) {
             }
         }
    }
 
public void execute(JobExecutionContext arg0) throws JobExecutionException {
                System.out.println("FTP execut" );
 	try {
   FTPFunctions ftp = new FTPFunctions("Test", 21, "killkala", "test" );
   String remoteDirPath = "/Station1";
            String saveDirPath = "//192.168.7.191/web/FTP";
 
            FTPFunctions.downloadDirectory(ftpClient, remoteDirPath, "", saveDirPath);
   System.out.println("FTP File downloaded successfully" );
   ftp.disconnect();
    } catch (Exception e) {
     e.printStackTrace();
    }     	 
}
} | 
Partager