FTP Upload in Java   Leave a comment

In this article we will learn how we can perform FTP upload to various FTP sites from java application. To perform FTP upload we would be using Apache Commons Net Library.

Package Requirement:
Java IO – Included in JDK
FTPClient – Apache Commons Net

Connecting to FTP Server

   String hostName = "www.google.com";
   String username = "test";
   String password = "test";
   String location = "D:\\test.mp3";

   FTPClient ftp = null;

   ftp = new FTPClient();
   ftp.connect(hostName);
   ftp.login(username, password);

Here we have specified all the important credentials and trying to connect to FTP server.

Setting Type Of File to Upload

   ftp.setFileType(FTP.BINARY_FILE_TYPE);

Ensure that you specify the type of file you want to upload. If we do not specify the file type than we are able to connect but the file is not uploaded to destination.

Changing Working Directory

   ftp.changeWorkingDirectory("/hiteshagrawal.com");

By default root directory is selected if you want another directory than change it.

Checking Whether FTP Connection was Successful

   int reply = ftp.getReplyCode();
   if(FTPReply.isPositiveCompletion(reply)){
      System.out.println("Connected Success");
      System.out.println("Write File Upload logic");
   }

Above code will ensure that connection is successful, now we are ready to upload file.

Uploading File to FTP Server

   File f1 = new File(location);
   in = new FileInputStream(f1);

   ftp.storeFile("test.mp3",in);

Disconnecting From FTP Connection

   ftp.logout();

NOTE
Remember to specify the file type, if you do not specify than uploading of file may not happen. To sum up below is the full code

   import java.io.File;
   import java.io.FileInputStream;
   import java.io.InputStream;

   import org.apache.commons.net.ftp.FTP;
   import org.apache.commons.net.ftp.FTPClient;
   import org.apache.commons.net.ftp.FTPReply;

   public class FTPUpload {

      public static void main(String args[]) {
         String hostName = "www.google.com";
         String username = "test";
         String password = "test";
         String location = "D:\\test.mp3";
         FTPClient ftp = null;

         InputStream in = null;
         try {
             ftp = new FTPClient();
	     ftp.connect(hostName);
	     ftp.login(username, password);

	     ftp.setFileType(FTP.BINARY_FILE_TYPE);

	     ftp.changeWorkingDirectory("/hiteshagrawal.com");	

	     int reply = ftp.getReplyCode();
	     System.out.println("Received Reply from FTP Connection:" + reply);

	     if(FTPReply.isPositiveCompletion(reply)){
	         System.out.println("Connected Success");
	     }

	     File f1 = new File(location);
	     in = new FileInputStream(f1);

	     ftp.storeFile("test.mp3",in);

	     System.out.println("SUCCESS");

	     ftp.logout();
             ftp.disconnect();
	 } catch (Exception e) {	
	    e.printStackTrace();
	 }
    }
}

http://www.hiteshagrawal.com/java/ftp-upload-in-java

 

Posted 2011年02月10日 by gw8310 in 未分类

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: