The Java or JDK comes with NO support of any official API for FTP connection. But there is an internal that comes in the sun.net package, but this is NOT well-documented, and you will hardly find how to use other methods within the sun.net.ftp.FtpClient class.
The sun.net.ftp.FtpClient actually sticks to the RFC959. But once you use this to build the class within your Java program, you will always get the complain that this internal API is likely to be discarded in the future versions of JDK.
But Java has the implementation of RFC1738, which is the URL spec, that can be used for initiating FTP connections, download and upload files. In order to be free from the hassle that an upgrade of the JDK might get your program to fail to work, due to it uses sun.net.ftp.FtpClient, therefore it’s best to use the URL spec. I’ve got a simple Java file that does the simple FTP connection, and opens an InputStream for downloading file, or opens an OutputStream for uploading file as follows:
And to use the above class for downloading or uploading file, you just need the simple few lines of code as follows:
1. Initiate the FTP connection first as follows, set the FTP server host name or IP address, and the user name and password, and call the connect method to connect to the FTP server
2. If you wanna upload a file, you need to set the file path relative to the FTP user’s home directory. For example, to upload a file into the sub folder “public_html” and the file to be named/saved as “u.txt”
3. Similarly, if you wanna download a file, you need to set the file path relative to the FTP user’s home directory. For example, to download a file from the sub folder “download” and the file to be downloaded is “dl.txt”