package helloFTP;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class helloFTP {
public static void main(String[] args) {
FTPClient client = new FTPClient();
FileInputStream fis = null;
String lb = "<<IPAddress>>";
try{
client.disconnect();
client.connect(lb,21);
boolean login = client.login("<FTPUserName>","<FTPPassword>");
if(login){restremo
System.out.println("login successful!");
System.out.println("client connection mode:"+client.getDataConnectionMode());
System.out.println("passive port:"+client.getPassivePort());
System.out.println("passive host:"+client.getPassiveHost());
System.out.println("default port:"+client.getDefaultPort());
System.out.println("connection time out:"+client.getConnectTimeout());
client.enterLocalActiveMode();
System.out.println("entered active mode");
String filename = "c:\\Folder\\test.txt";
PrintWriter out = new PrintWriter(filename);
String timenow = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
out.println(timenow);
out.close();
fis = new FileInputStream(filename);
client.setFileType(FTP.BINARY_FILE_TYPE,FTP.BINARY_FILE_TYPE);
client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
client.changeWorkingDirectory("/test");
if(client.storeFile("test.txt", fis))
{
System.out.println("file uploaded successfully");
}
else
client.logout();
;
boolean logout = client.logout();
if(logout)
{
System.out.println("log out from FTP Server");
}
}else
System.out.println("log in failed");
}
catch (IOException e)
{
e.printStackTrace();
}finally {
try{
client.disconnect();
}catch (IOException e){
e.printStackTrace();
}
}
}
}