Apache Commons Net™ library implements the client side of many basic Internet protocols. The purpose of the library is to provide fundamental protocol access, not higher-level abstractions.
In this article we are going to use the Apache Commons Net API to upload a file to from local machine to a remote FTP server, the needed steps are:
1 - Creating a new FTP Client from the Apache Commons Net API
2 - Connect to the remote FTP server
3 - Set the files transfer mode (ASCII, Binary...etc), we will be using Binary mode
4 - Enable Data transfer between the local machine and the remote FTP server
5 - Send Login Command
6 - Do File transfer
7 - Logout from the remote FTP server
8 - Disconnect from the remote FTP server
The below code is the Java translation of the previous steps :
FTPClient client = new FTPClient();
client.connect("Your FTP Server");
client.setFileType(FTP.BINARY_FILE_TYPE);
client.enterLocalPassiveMode();
client.login("Your Username", "Your Password");
InputStream input = new FileInputStream(new File("Local File Location + File Name (i.e. C:/Users/adevedo/Desktop/myfile.dat"));
client.storeFile("Remote File Location + File Name (i.e. /www/location/myfile.dat", input);
client.logout();
client.disconnect();
Read more: Adevedo
QR: