Thursday, October 10, 2013

Upload file to FTP server

In this post, you learn to write Java code to upload a file from your local disk to a remote FTP server. Java does not provide an API to connect and upload files to or download files from a remote FTP server. In this example code, the API used to connect and upload the file to the FTP server is commons-net. The API contains a class called FTPClient that can be used to upload files to or download files from the FTP server. The commons-net API can be downloaded from Apache website.

In the example code below, the FTPClient object is created and its connect(String ftphostname) is invoked to connect to the remote FTP server. You need to supply the name of your FTP server to this method. The login(String username,String password) method will log in to the remote server. Normally, for file transferring between the client and server, you need to set the connection mode to passive mode by using the enterLocalPassiveMode() method. For the file transferring between a server and another server, you will use the enterRemoteActiveMode() instead. The default directory of the server to upload the file to is the root directory (/). You can specify a directory to upload the file to by using the changeWorkingDirectory(String dirpath).
Before transferring the files to the server, you should check whether you connect and login to the server successfully by using the isPositiveCompletion(int replyCode) method. If this method return true, you can upload the files to the server by using the storeFile(String remoteFileName,InputStream is) method. This method will copy data from the InputStream object and place it on the server with the name specified by the remoteFileName argument. After the upload task completes, you can log out and disconnect from the server by using the logout() and disconnect() method.


public class HttpHeaders {
public static void main(String[] args){


public static void ftpUpload(){

FTPClient client = new FTPClient();
FileInputStream fis = null;

try {
   //connect to the remove ftp server
   //log in to the server with user name and password
   client.login("yourusername", "yourpassword");
   //set the passive mode for the file transfer
   //upload file to this directory
   int reply=client.getReplyCode();
   String filename = "d:/SQLiteJava.txt";  
   fis = new FileInputStream(filename);  
   if(FTPReply.isPositiveCompletion(reply)){ //connect and login successfully
    //upload file to the server
    client.storeFile("SQLiteJava.txt", fis);
    System.out.println("Can't upload to the FTP server.");
   //log out from the server

} catch (Exception e) {
} finally {
   try {
       if (client.isConnected()) {
        //disconnect from the server
   } catch (IOException e) {

If you use Java 7 and Windows 7 or Vista, there is a common problem to transfer the file between the client and the remove FTP server. This problem causes the file transfer failed. You will get the socket exception as shown below.

To avoid this problem or error, you need to turn off the Windows Firewall (Control Panel-> Windows Firewall). Then run the the Java program again.

compass app flashLight app


  1. Thanks for your informative article on ios mobile application development. Your article helped me to explore the future of mobile apps developers. Having sound knowledge on mobile application development will help you to float in mobile application development. iOS Training in Chennai | iOS Training Institutes in Chennai| Android Training in Chennai

  2. Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.

    Android Training in chennai |
    Android course in chennai |
    IOS Training in chennai

  3. This is really interesting, You're a very skilled blogger. I've joined your feed and look forward to seeking more of your great post. Also, I've shared your website in my social networks! solar window film

  4. Great post! I didn’t knowral of these resources and I will dowload it now!

  5. Taldeen is one of the best plastic manufacturing company in Saudi Arabia. They are manufacturing Handling Solutions Plastic products like Plastic Pallets and plastic crates. Here is the link of the product
    Handling Solutions
    Plastic Pallets
    GrueBleen is one of the Branding and Marketing agency Based in Riyadh- Saudi Arabia. The main functions of GrueBleen is Advertising, Branding, Marketing, Office Branding, Exhibition Management and Digital Marketing. Visit the below link to know more about GrueBleen Creative Club.
    Branding Agency Riyadh
    Marketing Agency Riyadh
    Agriculture Solutions – Taldeen is a plastic manufacturing company in Saudi Arabia. They are manufacturing agricultural plastic products like greenhouse cover and hay cover. Visit the below link to know more details
    Agriculture Solutions
    Greenhouse Cover
    GrueBleen – One of the best social media marketing agency in Riyadh- Saudi Arabia. Visit here for the all service details of GrueBleen.
    Social Media Marketing Agency | Social Media Agency In Saudi Arabia | Social Media Agency In Riyadh | Social Media Agency in Jeddah |