Thank you to those who tried to help. I found the answer to my problem here. this is How I did it:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FTPExample {
public static void main(String[] args) {
try {
//new ftp client
FTPClient ftp = new FTPClient();
//try to connect
ftp.connect("MyHhostName");
//login to server
if (!ftp.login("username", "password")) {
ftp.logout();
}
int reply = ftp.getReplyCode();
//FTPReply stores a set of constants for FTP reply codes.
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
//enter passive mode
ftp.enterLocalPassiveMode();
//get system name
System.out.println("Remote system is " + ftp.getSystemType());
//change current directory
ftp.changeWorkingDirectory("/App/PMIGENV/BACK/Finacle/FC/app/CDCI_LOGS/log/UBSADMIN");
System.out.println("Current directory is " + ftp.printWorkingDirectory());
//get list of filenames
FTPFile[] ftpFiles = ftp.listFiles();
if (ftpFiles != null && ftpFiles.length > 0) {
//loop thru files
for (FTPFile file : ftpFiles) {
if (!file.isFile()) {
continue;
}
System.out.println("File is " + file.getName());
//get output stream
OutputStream output;
output = new FileOutputStream("FtpFiles" + "/" + file.getName());
//get the file from the remote system
ftp.retrieveFile(file.getName(), output);
//close output stream
output.close();
//delete the file
// ftp.deleteFile(file.getName());
}
}
ftp.logout();
ftp.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}