All Packages Class Hierarchy This Package Previous Next Index
Class com.ibm.as400.access.FTP
java.lang.Object
|
+----com.ibm.as400.access.FTP
- public class FTP
- extends Object
- implements Serializable
The FTP class represents a generic ftp client. Methods
on the FTP class allow you to connect to an ftp server,
send commands to the server, list files on the server,
get files from the server, and put files to the server.
Most methods that communicate with the server return a boolean
to indicate if the request was successful. The message returned
from the server is also available. getLastMessage() is used
to retrieve the message from the previous request.
By default, FTP command are sent via server port 21. The initial
data transfer type is ASCII. Passive mode is used.
No encryption is provided by this class. The user and password
flow un-encrypted to the server. This class is not SSL enabled.
The forward slash is the separator character for paths sent
to the FTP server.
Trace information is available by using the com.ibm.as400.access.Trace
class. When trace is turned on via that class, FTP will
produce debug information.
The following example copies a set of files from a directory
on the server.
FTP client = new FTP("myServer", "myUID", "myPWD");
client.cd("/myDir");
client.setDataTransferType(FTP.BINARY);
String [] entries = client.ls();
for (int i = 0; i < entries.length; i++)
{
System.out.println("Copying " + entries[i]);
try
{
client.get(entries[i], "c:\\ftptest\\" + entries[i]);
}
catch (Exception e)
{
System.out.println(" copy failed, likely this is a directory");
}
}
client.disconnect();
-
ASCII
- Transfer files in ASCII mode.
-
BINARY
- Transfer files in binary mode.
-
FTP()
- Constructs an FTP object.
-
FTP(String)
- Constructs an FTP object.
-
FTP(String, String, String)
- Constructs an FTP object.
-
addFTPListener(FTPListener)
- Adds a listener to be notified when an FTP event is fired.
-
addPropertyChangeListener(PropertyChangeListener)
- Adds a listener to be notified when the value of any bound property
is changed.
-
addVetoableChangeListener(VetoableChangeListener)
- Adds a listener to be notified when the value of any
constrained property is changed.
-
cd(String)
- Sets the current directory on the server to directory.
-
connect()
- Connects to the server.
-
dir()
- Lists the contents of the current working directory.
-
disconnect()
- Closes the connection with the server.
-
finalize()
- Closes all streams and sockets before this object
is garbage collected.
-
get(String)
- Starts the process of getting a file from the server.
-
get(String, File)
- Gets a file from the server.
-
get(String, String)
- Gets a file from the server.
-
getBufferSize()
- Returns the size of the buffer used when transferring files.
-
getCurrentDirectory()
- Returns the current directory on the server.
-
getLastMessage()
- Returns the text of the last reply returned from the server.
-
getPort()
- Returns the port used to connect to the system.
-
getServer()
- Returns the name of the server.
-
getUser()
- Returns the user.
-
issueCommand(String)
- Sends a command to the server, returning the reply from the server.
-
ls()
- Lists the contents of the current working directory.
-
noop()
- Sends the NOOP (no operation) command to the server.
-
put(File, String)
- Puts a file to the server.
-
put(String)
- Starts the process of putting a file to the server.
-
put(String, String)
- Puts a file to the server.
-
pwd()
- Returns the current directory on the server.
-
removeFTPListener(FTPListener)
- Removes a listener from the list.
-
removePropertyChangeListener(PropertyChangeListener)
- Removes this listener from being notified when a bound property changes.
-
removeVetoableChangeListener(VetoableChangeListener)
- Removes this listener from being notified when a constrained property changes.
-
setBufferSize(int)
- Sets the buffer size used when transferring files.
-
setCurrentDirectory(String)
- Sets the current directory on the server to directory.
-
setDataTransferType(int)
- Sets the data transfer type.
-
setPassword(String)
- Sets the password.
-
setPort(int)
- Sets the port to use when connecting to the server.
-
setServer(String)
- Sets the name of the server.
-
setUser(String)
- Sets the user identifier used when connecting to the server.
ASCII
public static final int ASCII
- Transfer files in ASCII mode.
BINARY
public static final int BINARY
- Transfer files in binary mode.
FTP
public FTP()
- Constructs an FTP object.
The server name, user and password must be set before
requests are sent to the server.
FTP
public FTP(String server)
- Constructs an FTP object.
The user and password must be set before requests are
sent to the server.
- Parameters:
- server - The system to which to connect.
FTP
public FTP(String server,
String user,
String password)
- Constructs an FTP object.
- Parameters:
- server - The system to which to connect.
- user - The userid to use during the login.
- password - The password to use during the login.
addPropertyChangeListener
public void addPropertyChangeListener(PropertyChangeListener listener)
- Adds a listener to be notified when the value of any bound property
is changed. It can be removed with removePropertyChangeListener.
- Parameters:
- listener - The PropertyChangeListener.
addFTPListener
public void addFTPListener(FTPListener listener)
- Adds a listener to be notified when an FTP event is fired.
- Parameters:
- listener - The object listener.
addVetoableChangeListener
public void addVetoableChangeListener(VetoableChangeListener listener)
- Adds a listener to be notified when the value of any
constrained property is changed.
- Parameters:
- listener - The VetoableChangeListener.
cd
public boolean cd(String directory) throws IOException
- Sets the current directory on the server to directory.
The method is the same as setCurrentDirectory().
The message returned from the server is saved. Use getLastMessage()
to retrieve it.
- Parameters:
- directory - The current directory to set on the server.
- Returns:
- true if directory changed; false otherwise.
- Throws: IOException
- If an error occurs while communicating with the server.
connect
public synchronized boolean connect() throws UnknownHostException, IOException, IllegalStateException
- Connects to the server. The connection is via
port port. The user and password must be set
before calling this method.
Calling connect is optional. Methods that communicate
with the server such as get, put, cd, and ls call connect()
if necessary.
The message returned from the server is saved. Use getLastMessage()
to retrieve it.
- Returns:
- true if connection is successful; false otherwise.
- Throws: UnknownHostException
- If a path to the server cannot be found.
- Throws: IOException
- If an error occurs while connecting to the server.
- Throws: IllegalStateException
- If called before user and password are set.
dir
public String[] dir() throws IOException
- Lists the contents of the current working directory. File name and
attributes are returned for each entry in the directory.
An array of length zero is returned if the directory is empty.
- Returns:
- The contents of the directory as an array of Strings.
- Throws: IOException
- If an error occurs while communicating with the server.
disconnect
public synchronized void disconnect() throws IOException
- Closes the connection with the server. The connection is closed
by sending the quit command to the server.
The message returned from the server is saved. Use getLastMessage()
to retrieve it.
- Throws: IOException
- If an error occurs while communicating with the server.
finalize
protected void finalize() throws Throwable
- Closes all streams and sockets before this object
is garbage collected.
- Throws: Throwable
- If an error occurs during cleanup.
- Overrides:
- finalize in class Object
get
public InputStream get(String fileName) throws IOException, FileNotFoundException
- Starts the process of getting a file from the server. FTP
opens the data connection to the server, then opens the file on
the server and returns an input stream to the caller. The caller
reads the file's data from the input stream.
The source file is on the server, accessed via FTP so the path
separator character (if any) must be a forward slash.
- Parameters:
- fileName - The file to get.
- Returns:
- An input stream to the file. The caller uses the input
stream to read the data from the file.
Null is returned if the connection to the server fails.
- Throws: IOException
- If an error occurs while communicating with the server.
- Throws: FileNotFoundException
- If the name is a directory or the name is not found.
get
public boolean get(String sourceFileName,
String targetFileName) throws IOException, FileNotFoundException
- Gets a file from the server.
The source file is on the server, accessed via FTP so the path
separator character (if any) must be a forward slash.
The target file is on the client, accessed via java.io
so the path separator character (if any) must be client specific.
- Parameters:
- sourceFileName - The file to get on the server.
- targetFileName - The file on the target file system.
- Returns:
- true if the copy was successful; false otherwise.
- Throws: IOException
- If an error occurs while communicating with the server.
- Throws: FileNotFoundException
- If the source file or the targe file
cannot be accessed.
get
public boolean get(String sourceFileName,
File targetFile) throws IOException, FileNotFoundException
- Gets a file from the server.
The source file is on the server, accessed via FTP so the path
separator character (if any) must be a forward slash.
The target file is an instance of Java.io.file.
- Parameters:
- sourceFileName - The file to get on the server.
- targetFileName - The file on the target file system.
- Returns:
- true if the copy was successful; false otherwise.
- Throws: IOException
- If an error occurs while communicating with the server.
- Throws: FileNotFoundException
- If the source file or the targe file
cannot be accessed.
getBufferSize
public int getBufferSize()
- Returns the size of the buffer used when transferring files.
When this class copies data between the source file and target file,
a buffer of this size is used. The default buffer size is 4096 bytes.
- Returns:
- The buffer size used when transferring files.
getCurrentDirectory
public String getCurrentDirectory() throws IOException
- Returns the current directory on the server.
- Returns:
- The current directory on the server.
Null is returned if the connection to the server fails.
- Throws: IOException
- If an error occurs while communicating with the server.
getLastMessage
public synchronized String getLastMessage()
- Returns the text of the last reply returned from the server.
Empty string is returned if no request has been sent.
- Returns:
- The text of the last reply returned from the server.
getPort
public int getPort()
- Returns the port used to connect to the system.
- Returns:
- The port used to connect to the system.
getServer
public String getServer()
- Returns the name of the server. Null is returned if no system has
been set.
- Returns:
- The name of the server to which this object connects.
getUser
public String getUser()
- Returns the user. Null is returned if no user has
been set.
- Returns:
- The name of the user.
issueCommand
public synchronized String issueCommand(String cmd) throws IOException
- Sends a command to the server, returning the reply from the server.
The command is not altered before sending it to the server so it
much be recognized by the server. Many FTP applications change
commands so they are recognized by the server. For example, the
command to get a list of files from the server is NLST, not ls. Many
FTP applications convert ls to NLST before sending the command to
the server. This method will not do the conversion.
- Parameters:
- command - The command to send to the server.
- Returns:
- The reply to the command.
Null is returned if the connection to the server fails.
- Throws: IOException
- If an error occurs while communicating with the server.
ls
public String[] ls() throws IOException
- Lists the contents of the current working directory. If the directory
is empty, an empty list is returned.
- Returns:
- The contents of the directory as an array of Strings.
- Throws: IOException
- If an error occurs while communicating with the server.
noop
public boolean noop() throws IOException
- Sends the NOOP (no operation) command to the server. This
request is most useful to see if the connection to the server
is still active
- Returns:
- true if the request was successful, false otherwise.
- Throws: IOException
- If an error occurs while communicating with the server.
put
public synchronized OutputStream put(String fileName) throws IOException
- Starts the process of putting a file to the server. FTP
opens the data connection to the server, then opens the file on
the server and returns an output stream to the caller. The caller
then writes the file's data to the output stream.
- Parameters:
- fileName - The file to put.
- Returns:
- An output stream to the file. The caller uses the output
stream to write data to the file.
Null is returned if the connection to the server fails.
- Throws: IOException
- If an error occurs while communicating with the server.
put
public synchronized boolean put(String sourceFileName,
String targetFileName) throws IOException
- Puts a file to the server.
- Parameters:
- sourceFileName - The file to put.
- targetFileName - The file on the server.
- Returns:
- true if the copy was successful; false otherwise.
- Throws: IOException
- If an error occurs while communicating with the server.
put
public synchronized boolean put(File sourceFileName,
String targetFileName) throws IOException
- Puts a file to the server.
- Parameters:
- sourceFileName - The file to put.
- targetFileName - The file on the server.
- Returns:
- true if the copy was successful; false otherwise.
- Throws: IOException
- If an error occurs while communicating with the server.
pwd
public synchronized String pwd() throws IOException
- Returns the current directory on the server. PWD is the ftp
command Print Working Directory.
- Returns:
- The current directory on the server.
Null is returned if the connection to the server fails.
- Throws: IOException
- If an error occurs while communicating with the server.
removePropertyChangeListener
public void removePropertyChangeListener(PropertyChangeListener listener)
- Removes this listener from being notified when a bound property changes.
- Parameters:
- listener - The PropertyChangeListener.
removeFTPListener
public void removeFTPListener(FTPListener listener)
- Removes a listener from the list.
- Parameters:
- listener - The FTP listener.
removeVetoableChangeListener
public void removeVetoableChangeListener(VetoableChangeListener listener)
- Removes this listener from being notified when a constrained property changes.
- Parameters:
- listener - The VetoableChangeListener.
setBufferSize
public synchronized void setBufferSize(int bufferSize) throws PropertyVetoException
- Sets the buffer size used when transferring files. The default
buffer size is 4096 bytes.
- Parameters:
- size - The size of the buffer used when transferring files.
- Throws: PropertyVetoException
- If the change is vetoed.
setCurrentDirectory
public synchronized boolean setCurrentDirectory(String directory) throws IOException
- Sets the current directory on the server to directory.
The method is the same as cd().
The message returned from the server is saved. Use getLastMessage()
to retrieve it.
- Parameters:
- directory - The current directory to set on the server.
- Returns:
- true if directory changed; false otherwise.
- Throws: IOException
- If an error occurs while communicating with the server.
setDataTransferType
public void setDataTransferType(int transferType) throws IOException
- Sets the data transfer type. Valid values are:
If a connection does not
already exist, a connection is made to the server.
The message returned from the server is saved. Use getLastMessage()
to retrieve it.
- Throws: IOException
- If an error occurs while communicating with the server.
setPassword
public synchronized void setPassword(String password)
- Sets the password. The password cannot be changed once
a connection is made to the server.
- Parameters:
- The - password for the user.
setPort
public synchronized void setPort(int port) throws PropertyVetoException
- Sets the port to use when connecting to the server.
The port cannot be changed once
a connection is made to the server.
- Parameters:
- Port - The port to use when connecting to the server.
- Throws: PropertyVetoException
- If the change is vetoed.
setServer
public synchronized void setServer(String server) throws PropertyVetoException
- Sets the name of the server. The system name cannot be changed once
a connection is made to the server.
- Parameters:
- The - name of the server to which this object connects.
- Throws: PropertyVetoException
- If the change is vetoed.
setUser
public synchronized void setUser(String user) throws PropertyVetoException
- Sets the user identifier used when connecting to the server.
If the client is connected to the server, this method
will disconnect the connection
- Parameters:
- user - The user identifier used when connecting to the server.
- Throws: PropertyVetoException
- If the change is vetoed.
- Throws: IllegalStateException
- If connection already established to the server.
All Packages Class Hierarchy This Package Previous Next Index