Upload and Download file from FTP Server using Java FTP Client

Recently within my project, I stumbled upon a requirement where I had to test one of the application’s functionality to upload/download the file via FTP Server. It was pretty much straight forward to do the same manually by using any of the FTP Client for example Filezilla but equally difficult to automate the process. Thus, after researching a bit, I found Java FTP Client which makes it a task of just method calls.

Thus, though this blog, I would make you acquainted with Java FTP Client API, its basic methods, its usefulness and the code sample that can be readily used by the readers within their project. Therefore, today we would “Upload and Download file from FTP Server using Java FTP Client”.





Before imitating with the code, there is one library that needs to be downloaded and configured within the project’s build path to be able to access the methods of Java FTP Client. Refer the following steps:

  • Download the zipped “Apache Commons Net” folder from here.
  • Extract the folder at any desired location on to your file system.
  • Configure the project and add the “commons-net-3.3.jar” file available within the downloaded folder as external library.

FTP Server Uplaod and Download

Let us now motion towards the sample code for the following functionalities:

  • Upload a file on the FTP Server
  • Download a file from the FTP Server
  • List the file in a particular directory located at the FTP Server and verify the presence of a particular file.

Sample Code





Before developing the code for the aforementioned functionalities, we would create a java class “FTPFunctions.java” under which we would define all the functionalities. Check out the code below:

The code above is self explanatory. Just to brief a bit, refer the following points:

  • The constructor is created to connect with the FTP Server using host name and port number.
  • The upload method is created to upload the file to a particular directory on the FTP Server.
  • The download methods are created to download the file from the FTP Server to the local machine.
  • The list method is created to check if the particular file is present inside the specified directory on the FTP Server.

I hope your search ends here and the content is relevant with respect to your needs. Please add if you have any suggestions or advise on this blog.

8 comments

  • Great work!

  • Hi Shruti,

    Nice article, finally Even I got such requirement and I tried above code but I am facing some proxy issue in my organisation. I tried proxy class and proxy class in Selenium as well but did not find a way to overcome with it. Can you please guide me in this so that I can apply the same in my code on Monday

  • what is the problem of downloading files ..everytime i do it will be download a corrupted files… please give me a tips to fix it tnx…

  • Thanks so much for sharing this with us Shruti. It is very helpful. I have a question. What if I am using public/private key to connect to the server instead of password? What should I do? Any help would be definitely appreciated.

  • Nicely written. It helped. Thanks

  • Hi Shruti,
    Nice explanation , it will help for my project .
    I need your help for my project,
    do you know how to convert FTPFile format into File format(IO package)?
    Ex:
    FTPFile[] ftpFiles = fClient.listFiles();
    for(FTPFile ftpFile : allFiles) {
    // here ftpFile, I need fileFtp convert into File(IO package)
    }

  • Great article. I’m gonna write automated test checks that application upload file on ftp. I think it is what I was looking for. And maybe do you know some library for mock ftp server ?

Leave a Reply

Or

Your email address will not be published. Required fields are marked *