Below is sample code for FTP connection and upload/download file from server.
package sample;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class PropertyFTP {
String server = "127.0.0.1";// FTP server IP/Name
int port = 21;// Default FTP port
String user = "ftpuser"; //Username of your FTP account
String pass = "ftppass";// FTP Password
FTPClient ftpClient = null;
FTP ftp = null;
public PropertyFTP(){
ftpClient = new FTPClient();
ftp = new FTP();
}
public void conncetFtp(){
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setDirectory(String dir){
try {
ftpClient.changeWorkingDirectory("/"+dir);
} catch (Exception e) {
e.printStackTrace();
}
}
public void disconnetFtp(){
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
public void uploadFile(String fileName, InputStream inputStream){
try {
ftpClient.storeFile(fileName, inputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
public void downloadFile(String filename,OutputStream out){
try{
ftpClient.retrieveFile(filename, out);
}catch(Exception e){
e.printStackTrace();
}
}
}