dependencies {
implementation 'commons-net:commons-net:3.6'
}
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FTPUploader {
public static void main(String[] args) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(username, password);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String localFilePath = "path_to_local_file";
File localFile = new File(localFilePath);
String remoteFilePath = "path_to_remote_directory";
String remoteFileName = localFile.getName();
FileInputStream inputStream = new FileInputStream(localFile);
boolean uploaded = ftpClient.storeFile(remoteFilePath + remoteFileName, inputStream);
inputStream.close();
if (uploaded) {
} else {
}
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class FTPDownloader {
public static void main(String[] args) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(username, password);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String remoteFilePath = "path_to_remote_file";
String localFilePath = "path_to_local_directory";
FileOutputStream outputStream = new FileOutputStream(localFilePath);
boolean downloaded = ftpClient.retrieveFile(remoteFilePath, outputStream);
outputStream.close();
if (downloaded) {
} else {
}
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}