Tuesday, 25 June 2013

Here is an example to Upload Multiple Files(.jpg, .txt, .pdf, .doc etc) to Remote Server:



Hi, welcome,

        FTPClient encapsulates all the functionality necessary to store and retrieve files from an FTP server. This class takes care of all low level details of interacting with an FTP server and provides a convenient higher level interface. As with all classes derived from SocketClient, you must first connect to the server with connect before doing anything, and finally disconnect after you're completely finished interacting with the server. Then you need to check the FTP reply code to see if the connection was successful.

package mobileapplicatio;

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


//the following packages has to be downloaded in order to work with APIs supported by FTP
//LINK to download the packages Zip file

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
package UploadApplication;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
//To use FTP methods you have to download the following libraries
//Zip

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
  1. public class TestUpload {
  2.     public static void main(String[] args) {
  3.         String server = "RemoteServer.com";
  4.         int port = 21;
  5.         String user = "User";
  6.         String pass = "password";
  7.         FTPClient ftpClient = new FTPClient();
  8.         try {
  9.             ftpClient.connect(server, port);
  10.             ftpClient.login(user, pass);
  11.             ftpClient.enterLocalPassiveMode();

  12.             ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  13.             File firstLocalFile = new File("PATHTOFOLDER");
  14.             for (final File fileEntry : firstLocalFile.listFiles()) {
  15.                 if (fileEntry.isDirectory()) {
  16.                     //do your operatons;
  17.                 } else {
  18.                     String firstRemoteFile = fileEntry.getName();
  19.                     InputStream inputStream = new FileInputStream(fileEntry);

  20.                     System.out.println("Start uploading file-->"+fileEntry.getName());
  21.                     boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
  22.                     inputStream.close();
  23.                     if (done) {
  24.                         System.out.println("The first file is uploaded successfully.");
  25.                     }
  26.                 }
  27.             }
  28.         } catch (IOException ex) {
  29.             System.out.println("Error: " + ex.getMessage());
  30.             ex.printStackTrace();
  31.         } finally {
  32.             try {
  33.                 if (ftpClient.isConnected()) {
  34.                     ftpClient.logout();
  35.                     ftpClient.disconnect();
  36.                 }
  37.             } catch (IOException ex) {
  38.                 ex.printStackTrace();
  39.             }
  40.         }
  41.     }
  42. }

/*  Steps to add libraries

-->Goto projects
-->right click on Libraries         
            -->Select Add jar file/folders
            --->Browse to downloaded files and click ok

*/


***********************************************************************************************
Explanation for the client program


Line 3:Specify The Remote server address(URL) to which the file has to be uploaded.

Line 4:Default port number for FTP clients

Line 5:Specify User name

Line 6:Specify PASSWORD

Line 7: Creating instance of FTPClient in next three lines we will connect to the Server

Line 14: Specify the path of the folder from which the files will be selected and uploaded.

Line 15:Loop through each file present in  the specified library
              Upload each file with the same name.

Line 23:Uploads file to Remote server with the specified name.

*************************************************************************




Leave your doubts or suggestions in comments section...

2 comments: