import java.io.File;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.optional.net.FTP;
import org.apache.tools.ant.types.FileSet;
public class FtpUsingAnt {
public static void main(String[] args) {
ftpGetSample();
ftpDeleteTaskSample();
}
public static void ftpGetSample(){
FTP ftp=new FTP();
FTP.Action action=new FTP.Action();
action.setValue("get");
ftp.setAction(action);
ftp.setServer("server.com");
ftp.setUserid("user-name");
ftp.setPassword("password");
ftp.setRemotedir("/remote-folder");
FileSet fileSet=new FileSet();
File dir=new File("c:\\local-folder");
fileSet.setDir(dir);
fileSet.setIncludes("**/*.csv");
ftp.addFileset(fileSet);
callAntTask(ftp, "ftp");
}
public static void ftpDeleteTaskSample() {
FTP ftp=new FTP();
FTP.Action action=new FTP.Action();
action.setValue("del");
ftp.setAction(action);
ftp.setServer("server.com");
ftp.setUserid("user-name");
ftp.setPassword("password");
ftp.setRemotedir("/remote-folder");
FileSet fileSet=new FileSet();
File dir=new File("."); // Not Needed
fileSet.setDir(dir);
fileSet.setIncludes("**/*.csv");
ftp.addFileset(fileSet);
callAntTask(ftp, "ftp");
}
public static void callAntTask(Task task, String taskName){
DefaultLogger logger=new DefaultLogger();
logger.setMessageOutputLevel(Project.MSG_DEBUG);
logger.setOutputPrintStream(System.out);
logger.setErrorPrintStream(System.err);
Project project=new Project();
project.addBuildListener(logger);
task.setProject(project);
task.setTaskName(taskName);
task.execute();
}
}
Friday, June 26, 2009
Java FTP Sample Code
Writing FTP Code using ANT is very simple compare to we write the same using FTPClient class. This post will explain you how to do basic FTP operation through ANT FTP task classes.
Labels:
Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment