nu.staldal.ftp
Class FTPClient

java.lang.Object
  extended by nu.staldal.ftp.FTPClient

public class FTPClient
extends java.lang.Object

An FTP client. See RFC-959. Pathnames must be specified using '/' for directory separator. Passive mode will be used for all transfers. Not thread-safe, i.e. you cannot start a new file while another one is in progress. Note: This class will transmit password in clear text over the network.


Constructor Summary
FTPClient(java.lang.String host, int port, java.lang.String username, java.lang.String password, java.lang.String path)
          Connect to an FTP server and login.
FTPClient(java.lang.String url, java.lang.String password)
          Connect to an FTP server and login.
 
Method Summary
 java.io.OutputStream append(java.lang.String pathname)
          Create a new file, or append to an existing file.
 java.io.OutputStream append(java.lang.String pathname, boolean last)
          Create a new file, or append to an existing file.
 void close()
          Logout and disconnect from the FTP server.
 boolean deleteFile(java.lang.String pathname)
          Deletes a file.
 java.io.OutputStream store(java.lang.String pathname)
          Create a new file, or overwrite an existing file.
 java.io.OutputStream store(java.lang.String pathname, boolean last)
          Create a new file, or overwrite an existing file.
 java.io.OutputStream storeUnique(java.lang.String path)
          Create a new file with an unique name.
 java.io.OutputStream storeUnique(java.lang.String path, boolean last)
          Create a new file with an unique name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FTPClient

public FTPClient(java.lang.String url,
                 java.lang.String password)
          throws java.net.MalformedURLException,
                 java.net.UnknownHostException,
                 FTPAuthenticationException,
                 FTPException,
                 java.io.IOException
Connect to an FTP server and login.

Parameters:
url - an URL specifying host, port, username and optionally an initial path to change to
password - the password to use for logging in
Throws:
java.net.MalformedURLException - if there are any syntactic error in the URL
java.net.UnknownHostException - if the hostname specified doesn't exist
FTPAuthenticationException - if the password is wrong
FTPException - if any FTP protocol error occurs
java.io.IOException - if any other IO error occurs

FTPClient

public FTPClient(java.lang.String host,
                 int port,
                 java.lang.String username,
                 java.lang.String password,
                 java.lang.String path)
          throws java.net.UnknownHostException,
                 FTPAuthenticationException,
                 FTPException,
                 java.io.IOException
Connect to an FTP server and login.

Parameters:
host - the host to connect to, may be a domain name or IP address
port - the control channel port (default 21)
username - the username to use for logging in
password - the password to use for logging in
path - initial path, realtive to home directory unless starting with '/', may be null to use home directory
Throws:
java.net.UnknownHostException - if the hostname specified doesn't exist
FTPAuthenticationException - if the password is wrong
FTPException - if any FTP protocol error occurs
java.io.IOException - if any other IO error occurs
Method Detail

close

public void close()
           throws java.io.IOException
Logout and disconnect from the FTP server. After this method has been invoked, no other method may be invoked.

Throws:
java.io.IOException

store

public java.io.OutputStream store(java.lang.String pathname)
                           throws FTPException,
                                  java.io.IOException
Create a new file, or overwrite an existing file. Will create directories as nessesary.

Parameters:
pathname - path to the file
Returns:
an OutputStream to write to, close() it when finished
Throws:
FTPException - if any FTP protocol error occurs
java.io.IOException - if any other IO error occurs

store

public java.io.OutputStream store(java.lang.String pathname,
                                  boolean last)
                           throws FTPException,
                                  java.io.IOException
Create a new file, or overwrite an existing file. Will create directories as nessesary.

Parameters:
pathname - path to the file
last - close the FTPClient after transferring this file
Returns:
an OutputStream to write to, close() it when finished
Throws:
FTPException - if any FTP protocol error occurs
java.io.IOException - if any other IO error occurs

append

public java.io.OutputStream append(java.lang.String pathname)
                            throws FTPException,
                                   java.io.IOException
Create a new file, or append to an existing file. Will create directories as nessesary.

Parameters:
pathname - path to the file
Returns:
an OutputStream to write to, close() it when finished
Throws:
FTPException - if any FTP protocol error occurs
java.io.IOException - if any other IO error occurs

append

public java.io.OutputStream append(java.lang.String pathname,
                                   boolean last)
                            throws FTPException,
                                   java.io.IOException
Create a new file, or append to an existing file. Will create directories as nessesary.

Parameters:
pathname - path to the file
last - close the FTPClient after transferring this file
Returns:
an OutputStream to write to, close() it when finished
Throws:
FTPException - if any FTP protocol error occurs
java.io.IOException - if any other IO error occurs

storeUnique

public java.io.OutputStream storeUnique(java.lang.String path)
                                 throws FTPException,
                                        java.io.IOException
Create a new file with an unique name. Will create directories as nessesary.

Parameters:
path - path to the file, must end with '/' or be empty
Returns:
an OutputStream to write to, close() it when finished
Throws:
FTPException - if any FTP protocol error occurs
java.io.IOException - if any other IO error occurs

storeUnique

public java.io.OutputStream storeUnique(java.lang.String path,
                                        boolean last)
                                 throws FTPException,
                                        java.io.IOException
Create a new file with an unique name. Will create directories as nessesary.

Parameters:
path - path to the file, must end with '/' or be empty
last - close the FTPClient after transferring this file
Returns:
an OutputStream to write to, close() it when finished
Throws:
FTPException - if any FTP protocol error occurs
java.io.IOException - if any other IO error occurs

deleteFile

public boolean deleteFile(java.lang.String pathname)
                   throws FTPException,
                          java.io.IOException
Deletes a file.

Parameters:
pathname - path to the file
Returns:
true if successful, false otherwise (file didn't exsist)
Throws:
FTPException - if any FTP protocol error occurs
java.io.IOException - if any other IO error occurs