We provide complete mobile and web apps development solutions

Tuesday, November 6, 2012

Upload file to server FTP

Class FTPClient
java.lang.Object
  extended by org.apache.commons.net.SocketClient
      extended by org.apache.commons.net.ftp.FTP
          extended by org.apache.commons.net.ftp.FTPClient


 public abstract class SocketClient extends Object


1. SocketClient provides the basic operations that are required of client objects accessing sockets. It is to be subclassed to avoid to rewrite the same code over and over again to open a socket, close a socket, set time outs etc.
2.setSocketFactory method, which allows you to control the type of Socke,the SocketClient creates for initiating network connections.

3. you could create a SocketFactory that requests browser security capabilities before creating a socket. All classes derived from SocketClient should use the _socketFactory_  member variable to create Socket and ServerSocket instances rather than instantiating them by directly invoking a constructor

4. The user will always be able to provide his own Socket implementations by substituting his own SocketFactory.

protected  void connect(InetAddress host)

Opens a Socket connected to a remote host at the current default port and originating from the current host at a system assigned port.

protected  void disconnect()
 Disconnects the socket connection.



public class FTP extends SocketClient


1. FTP provides the basic the functionality necessary to implement your own FTP client. It extends org.apache.commons.net.SocketClient since extending TelnetClient was causing unwanted behavior (like connections that did not time out properly).
2.  The FTPClient class, derived from FTP, implements all the functionality required of an FTP client. The FTP class is made public to provide access to various FTP constants and to make it easier for adventurous programmers (or those with special needs) to interact with the FTP protocol and implement their own clients. A set of methods with names corresponding to the FTP command names are provided to facilitate this interaction.
 3. You should keep in mind that the FTP server may choose to prematurely close a connection if the client has been idle for longer than a given time period (usually 900 seconds).
4. The FTP class will detect a premature FTP server connection closing when it receives a FTPReply.SERVICE_NOT_AVAILABLE  response to a command. When that occurs, the FTP class method encountering that reply will throw an FTPConnectionClosedException . FTPConectionClosedException is a subclass of  IOException  and therefore need not be caught separately, but if you are going to catch it separately, its catch block must appear before the more general  IOException  catch block.

5.  When you encounter an FTPConnectionClosedException , you must disconnect the connection with disconnect()  to properly clean up the system resources used by FTP. Before disconnecting, you may check the last reply code and text with getReplyCode , getReplyString , and getReplyStrings.

6. You may avoid server disconnections while the client is idle by periodicaly sending NOOP commands to the server.
 Rather than list it separately for each method, we mention here that every method communicating with the server and throwing an IOException can also throw a MalformedServerReplyException , which is a subclass of IOException. A MalformedServerReplyException will be thrown when the reply received from the server deviates enough from the protocol specification that it cannot be interpreted in a useful manner despite attempts to be as lenient as possible.


static int BINARY_FILE_TYPE

 A constant used to indicate the file(s) being transfered should be treated as a binary image, i.e., no translations should be performed.


static int BLOCK_TRANSFER_MODE
           A constant used to indicate a file is to be transfered as a series of blocks.

static int  TELNET_TEXT_FORMAT

A constant used to indicate a text file contains format vertical format control characters.


Ex:

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

 public class Main {
   public static void main(String[] args) {
     FTPClient client = new FTPClient();
     FileInputStream fis = null;

     client.connect("www.mobengineers.com");
     client.login("srinivas", "android");

     String filename = "mobengineers.dat";
     fis = new FileInputStream(filename);
     client.storeFile(filename, fis);
     client.logout();
     fis.close();
   }
 }







source:
http://commons.apache.org/net/api-3.1/org/apache/commons/net/ftp/FTPClient.html#storeFile(java.lang.String, java.io.InputStream)

0 coment�rios:

Post a Comment

Online Training

Your Name :
Your Email: (required)
Your Message: (required)

Powered by Blogger.

Recent Posts

Find Us On Facebook

Popular Posts