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.

import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;


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

}


public static void ftpUpload(){

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

try {
   //connect to the remove ftp server
    client.connect("ftp.worldbestlearningcenter.com");
   //log in to the server with user name and password
   client.login("yourusername", "yourpassword");
   //set the passive mode for the file transfer
   client.enterLocalPassiveMode();
   //upload file to this directory
   client.changeWorkingDirectory("/www");
   System.out.println(client.getReplyString());
   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);
       
   }
   else{
    System.out.println("Can't upload to the FTP server.");
   }
   //log out from the server
   client.logout();
 

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

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

3 comments:

  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

    ReplyDelete
  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

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

    ReplyDelete