I used commons net FTP to transfer a file from one server to another.
Maven dependency:
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
Java:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public void tranferFile() {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(servername, port);
ftpClient.login(username, password);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
File sourceFile = new File("file which you want to send");
InputStream inputStream = new FileInputStream(sourceFile);
boolean done = ftpClient.storeFile("filename which receiver get", inputStream);
inputStream.close();
if (done) {
LOGGER.info("file is uploaded successfully..............");
}
} catch (IOException e) {
LOGGER.error("Exception occured while ftp : "+e);
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException e) {
LOGGER.error("Exception occured while ftp logout/disconnect : "+e);
}
}
}
source
share