public class FTP
extends java.lang.Object
implements java.io.Serializable
Most methods that communicate with the system return a boolean to indicate if the request was successful. The message returned from the system is also available. getLastMessage() is used to retrieve the message from the previous request.
By default, FTP commands are sent via 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 system. 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 system.
FTP client = new FTP("mysystem", "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();
Modifier and Type | Field and Description |
---|---|
static int |
ACTIVE_MODE
Use active mode transfers with the system.
|
static int |
ASCII
Transfer files in ASCII mode.
|
static int |
BINARY
Transfer files in binary mode.
|
static int |
PASSIVE_MODE
Use passive mode transfers with the system.
|
Constructor and Description |
---|
FTP()
Constructs an FTP object.
|
FTP(java.lang.String server)
Constructs an FTP object.
|
FTP(java.lang.String server,
java.lang.String user,
java.lang.String password)
Constructs an FTP object.
|
Modifier and Type | Method and Description |
---|---|
void |
addFTPListener(FTPListener listener)
Adds a listener to be notified when an FTP event is fired.
|
void |
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds a listener to be notified when the value of any bound property
is changed.
|
void |
addVetoableChangeListener(java.beans.VetoableChangeListener listener)
Adds a listener to be notified when the value of any
constrained property is changed.
|
boolean |
append(java.io.File sourceFileName,
java.lang.String targetFileName)
Appends data to a file on the system.
|
java.io.OutputStream |
append(java.lang.String fileName)
Starts the process of appending data to a file on the system.
|
boolean |
append(java.lang.String sourceFileName,
java.lang.String targetFileName)
Appends data to a file on the system.
|
boolean |
cd(java.lang.String directory)
Sets the current directory on the system to directory.
|
boolean |
connect()
Connects to the system.
|
java.lang.String[] |
dir()
Lists the contents of the current working directory.
|
java.lang.String[] |
dir(java.lang.String criteria)
Lists the contents of the current directory.
|
void |
disconnect()
Closes the connection with the system.
|
protected void |
finalize()
Closes all streams and sockets before this object
is garbage collected.
|
static java.lang.String |
generateNewName(java.lang.String fromName,
java.lang.String toName)
Returns a new file name constructed out of the old
file name and a simple expression containing asterisks.
|
java.io.InputStream |
get(java.lang.String fileName)
Starts the process of getting a file from the system.
|
boolean |
get(java.lang.String sourceFileName,
java.io.File targetFile)
Gets a file from the system.
|
boolean |
get(java.lang.String sourceFileName,
java.lang.String targetFileName)
Gets a file from the system.
|
int |
getBufferSize()
Returns the size of the buffer used when transferring files.
|
java.lang.String |
getCurrentDirectory()
Returns the current directory on the system.
|
java.lang.String |
getLastMessage()
Returns the text of the last reply returned from the system.
|
int |
getMode()
Returns the current transfer mode.
|
int |
getPort()
Returns the port used to connect to the system.
|
java.lang.String |
getServer()
Returns the name of the system.
|
java.lang.String |
getUser()
Returns the user.
|
boolean |
isReuseSocket()
Indicates whether the socket is reused for multiple file transfers, when in active mode.
|
java.lang.String |
issueCommand(java.lang.String cmd)
Sends a command to the system, returning the reply from the system.
|
java.lang.String[] |
ls()
Lists the contents of the current working directory.
|
java.lang.String[] |
ls(java.lang.String criteria)
Lists the contents of the current working directory.
|
boolean |
noop()
Sends the NOOP (no operation) command to the system.
|
boolean |
put(java.io.File sourceFileName,
java.lang.String targetFileName)
Puts a file to the system.
|
java.io.OutputStream |
put(java.lang.String fileName)
Starts the process of putting a file to the system.
|
boolean |
put(java.lang.String sourceFileName,
java.lang.String targetFileName)
Puts a file to the system.
|
java.lang.String |
pwd()
Returns the current directory on the system.
|
void |
removeFTPListener(FTPListener listener)
Removes a listener from the list.
|
void |
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes this listener from being notified when a bound property changes.
|
void |
removeVetoableChangeListener(java.beans.VetoableChangeListener listener)
Removes this listener from being notified when a constrained property changes.
|
int |
ren(java.lang.String fromName,
java.lang.String toName)
Renames one or more files on the system, according to a specified pattern.
|
void |
setBufferSize(int bufferSize)
Sets the buffer size used when transferring files.
|
boolean |
setCurrentDirectory(java.lang.String directory)
Sets the current directory on the system to directory.
|
void |
setDataTransferType(int transferType)
Sets the data transfer type.
|
void |
setMode(int mode)
Sets the transfer mode.
|
void |
setPassword(java.lang.String password)
Sets the password.
|
void |
setPort(int port)
Sets the port to use when connecting to the system.
|
void |
setReuseSocket(boolean reuse)
Indicates whether to reuse a socket for multiple file transfers, when in active mode.
|
void |
setServer(java.lang.String server)
Sets the name of the system.
|
void |
setTimeOut(int socketTimeOut) |
void |
setUser(java.lang.String user)
Sets the user identifier used when connecting to the system.
|
public static final int ASCII
public static final int BINARY
public static final int ACTIVE_MODE
public static final int PASSIVE_MODE
public FTP()
public FTP(java.lang.String server)
server
- The system to which to connect.public FTP(java.lang.String server, java.lang.String user, java.lang.String password)
server
- The system to which to connect.user
- The userid to use during the login.password
- The password to use during the login.public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
listener
- The PropertyChangeListener.public void addFTPListener(FTPListener listener)
listener
- The object listener.public java.io.OutputStream append(java.lang.String fileName) throws java.io.IOException
fileName
- The file to put.java.io.IOException
- If an error occurs while communicating with the system.public boolean append(java.lang.String sourceFileName, java.lang.String targetFileName) throws java.io.IOException
sourceFileName
- The file to put.targetFileName
- The file on the system.java.io.IOException
- If an error occurs while communicating with the system.public boolean append(java.io.File sourceFileName, java.lang.String targetFileName) throws java.io.IOException
sourceFileName
- The file to put.targetFileName
- The file on the system.java.io.IOException
- If an error occurs while communicating with the system.public void addVetoableChangeListener(java.beans.VetoableChangeListener listener)
listener
- The VetoableChangeListener.public boolean cd(java.lang.String directory) throws java.io.IOException
directory
- The current directory to set on the system.java.io.IOException
- If an error occurs while communicating with the system.public boolean connect() throws java.net.UnknownHostException, java.io.IOException, java.lang.IllegalStateException
java.net.UnknownHostException
- If a path to the system cannot be found.java.io.IOException
- If an error occurs while connecting to the system.java.lang.IllegalStateException
- If called before user and password are set.public java.lang.String[] dir() throws java.io.IOException
java.io.IOException
- If an error occurs while communicating with the system.public java.lang.String[] dir(java.lang.String criteria) throws java.io.IOException
criteria
- The search criteria.java.io.IOException
- If an error occurs while communicating with the system.public void disconnect() throws java.io.IOException
java.io.IOException
- If an error occurs while communicating with the system.protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
- If an error occurs during cleanup.public java.io.InputStream get(java.lang.String fileName) throws java.io.IOException, java.io.FileNotFoundException
fileName
- The file to get.java.io.IOException
- If an error occurs while communicating with the system.java.io.FileNotFoundException
- If the name is a directory or the name is not found.public boolean get(java.lang.String sourceFileName, java.lang.String targetFileName) throws java.io.IOException, java.io.FileNotFoundException
sourceFileName
- The file to get on the system.targetFileName
- The file on the target file system.java.io.IOException
- If an error occurs while communicating with the system.java.io.FileNotFoundException
- If the source file or the targe file
cannot be accessed.public boolean get(java.lang.String sourceFileName, java.io.File targetFile) throws java.io.IOException, java.io.FileNotFoundException
sourceFileName
- The file to get on the system.targetFile
- The file on the target file system.java.io.IOException
- If an error occurs while communicating with the system.java.io.FileNotFoundException
- If the source file or the targe file
cannot be accessed.public int getBufferSize()
public java.lang.String getCurrentDirectory() throws java.io.IOException
java.io.IOException
- If an error occurs while communicating with the system.public java.lang.String getLastMessage()
public int getMode()
public int getPort()
public java.lang.String getServer()
public java.lang.String getUser()
public boolean isReuseSocket()
setMode(int)
public java.lang.String issueCommand(java.lang.String cmd) throws java.io.IOException
The command is not altered before sending it to the system, so it
must be recognized by the system. Many FTP applications change
commands so they are recognized by the system. For example, the
command to get a list of files from the system is NLST, not ls. Many
FTP applications convert ls to NLST before sending the command to
the system. This method will not do the conversion.
Throws SecurityException if userid or password is invalid.
cmd
- The command to send to the system.java.io.IOException
- If an error occurs while communicating with the system.public java.lang.String[] ls() throws java.io.IOException
java.io.IOException
- If an error occurs while communicating with the system.public java.lang.String[] ls(java.lang.String criteria) throws java.io.IOException
criteria
- The search criteria.java.io.IOException
- If an error occurs while communicating with the system.public boolean noop() throws java.io.IOException
java.io.IOException
- If an error occurs while communicating with the system.public java.io.OutputStream put(java.lang.String fileName) throws java.io.IOException
fileName
- The file to put.java.io.IOException
- If an error occurs while communicating with the system.public boolean put(java.lang.String sourceFileName, java.lang.String targetFileName) throws java.io.IOException
sourceFileName
- The file to put.targetFileName
- The file on the system.java.io.IOException
- If an error occurs while communicating with the system.public boolean put(java.io.File sourceFileName, java.lang.String targetFileName) throws java.io.IOException
sourceFileName
- The file to put.targetFileName
- The file on the system.java.io.IOException
- If an error occurs while communicating with the system.public java.lang.String pwd() throws java.io.IOException
java.io.IOException
- If an error occurs while communicating with the system.public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
listener
- The PropertyChangeListener.public void removeFTPListener(FTPListener listener)
listener
- The FTP listener.public void removeVetoableChangeListener(java.beans.VetoableChangeListener listener)
listener
- The VetoableChangeListener.public void setBufferSize(int bufferSize) throws java.beans.PropertyVetoException
bufferSize
- The size of the buffer used when transferring files.java.beans.PropertyVetoException
- If the change is vetoed.public boolean setCurrentDirectory(java.lang.String directory) throws java.io.IOException
directory
- The current directory to set on the system.java.io.IOException
- If an error occurs while communicating with the system.public void setDataTransferType(int transferType) throws java.io.IOException
If a connection does not
already exist, a connection is made to the system.
The message returned from the system is saved. Use getLastMessage()
to retrieve it.
Throws SecurityException if userid or password is invalid.
transferType
- ASCII or BINARYjava.io.IOException
- If an error occurs while communicating with the system.public void setMode(int mode)
mode
- The mode. Valid values are ACTIVE_MODE
or PASSIVE_MODE
.public void setPassword(java.lang.String password)
password
- The password for the user.public void setPort(int port) throws java.beans.PropertyVetoException
port
- The port to use when connecting to the system.java.beans.PropertyVetoException
- If the change is vetoed.public void setReuseSocket(boolean reuse)
reuse
- If true, the socket is reused. If false, a new socket is created for each subsequent file transfer.setMode(int)
public void setServer(java.lang.String server) throws java.beans.PropertyVetoException
server
- The name of the system to which this object connects.java.beans.PropertyVetoException
- If the change is vetoed.public void setUser(java.lang.String user) throws java.beans.PropertyVetoException
user
- The user identifier used when connecting to the system.java.beans.PropertyVetoException
- If the change is vetoed.java.lang.IllegalStateException
- If connection already established to the system.public int ren(java.lang.String fromName, java.lang.String toName) throws java.io.IOException
For example:
fromName
- A pattern specifying the file(s) to be renamed.
The syntax of the pattern is similar to the syntax for ls()
.toName
- The new file name, or a simple pattern
describing how to construct the new name out of fromName.
toName can contain up to two asterisks, one on each side of the ".".java.io.IOException
- If an error occurs while communicating with the system.public static java.lang.String generateNewName(java.lang.String fromName, java.lang.String toName)
For example:
fromName
- The original file nametoName
- The new file name, or a simple pattern
describing how to construct the new name out of fromName.
toName can contain up to two asterisks, one on each side of the ".".java.lang.NullPointerException
- if the fromName or toName
parameters are null.java.lang.IllegalArgumentException
- if fromName or toName
is null, or if the toName contains an invalid expression.public void setTimeOut(int socketTimeOut)