|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.ibm.as400.access.FTP
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();
Field Summary | |
---|---|
static int |
ACTIVE_MODE
Use active mode transfers with the server. |
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 server. |
Constructor Summary | |
---|---|
FTP()
Constructs an FTP object. |
|
FTP(String server)
Constructs an FTP object. |
|
FTP(String server,
String user,
String password)
Constructs an FTP object. |
Method Summary | |
---|---|
void |
addFTPListener(FTPListener listener)
Adds a listener to be notified when an FTP event is fired. |
void |
addPropertyChangeListener(PropertyChangeListener listener)
Adds a listener to be notified when the value of any bound property is changed. |
void |
addVetoableChangeListener(VetoableChangeListener listener)
Adds a listener to be notified when the value of any constrained property is changed. |
boolean |
append(File sourceFileName,
String targetFileName)
Appends data to a file on the server. |
OutputStream |
append(String fileName)
Starts the process of appending data to a file on the server. |
boolean |
append(String sourceFileName,
String targetFileName)
Appends data to a file on the server. |
boolean |
cd(String directory)
Sets the current directory on the server to directory. |
boolean |
connect()
Connects to the server. |
String[] |
dir()
Lists the contents of the current working directory. |
String[] |
dir(String criteria)
Lists the contents of the current directory. |
void |
disconnect()
Closes the connection with the server. |
protected void |
finalize()
Closes all streams and sockets before this object is garbage collected. |
static String |
generateNewName(String fromName,
String toName)
Returns a new file name constructed out of the old file name and a simple expression containing asterisks. |
InputStream |
get(String fileName)
Starts the process of getting a file from the server. |
boolean |
get(String sourceFileName,
File targetFile)
Gets a file from the server. |
boolean |
get(String sourceFileName,
String targetFileName)
Gets a file from the server. |
int |
getBufferSize()
Returns the size of the buffer used when transferring files. |
String |
getCurrentDirectory()
Returns the current directory on the server. |
String |
getLastMessage()
Returns the text of the last reply returned from the server. |
int |
getMode()
Returns the current transfer mode. |
int |
getPort()
Returns the port used to connect to the system. |
String |
getServer()
Returns the name of the server. |
String |
getUser()
Returns the user. |
boolean |
isReuseSocket()
Indicates whether the socket is reused for multiple file transfers, when in active mode. |
String |
issueCommand(String cmd)
Sends a command to the server, returning the reply from the server. |
String[] |
ls()
Lists the contents of the current working directory. |
String[] |
ls(String criteria)
Lists the contents of the current working directory. |
boolean |
noop()
Sends the NOOP (no operation) command to the server. |
boolean |
put(File sourceFileName,
String targetFileName)
Puts a file to the server. |
OutputStream |
put(String fileName)
Starts the process of putting a file to the server. |
boolean |
put(String sourceFileName,
String targetFileName)
Puts a file to the server. |
String |
pwd()
Returns the current directory on the server. |
void |
removeFTPListener(FTPListener listener)
Removes a listener from the list. |
void |
removePropertyChangeListener(PropertyChangeListener listener)
Removes this listener from being notified when a bound property changes. |
void |
removeVetoableChangeListener(VetoableChangeListener listener)
Removes this listener from being notified when a constrained property changes. |
int |
ren(String fromName,
String toName)
Renames one or more files on the server, according to a specified pattern. |
void |
setBufferSize(int bufferSize)
Sets the buffer size used when transferring files. |
boolean |
setCurrentDirectory(String directory)
Sets the current directory on the server to directory. |
void |
setDataTransferType(int transferType)
Sets the data transfer type. |
void |
setMode(int mode)
Sets the transfer mode. |
void |
setPassword(String password)
Sets the password. |
void |
setPort(int port)
Sets the port to use when connecting to the server. |
void |
setReuseSocket(boolean reuse)
Indicates whether to reuse a socket for multiple file transfers, when in active mode. |
void |
setServer(String server)
Sets the name of the server. |
void |
setUser(String user)
Sets the user identifier used when connecting to the server. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int ASCII
public static final int BINARY
public static final int ACTIVE_MODE
public static final int PASSIVE_MODE
Constructor Detail |
public FTP()
public FTP(String server)
server
- The system to which to connect.public FTP(String server, String user, 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.Method Detail |
public void addPropertyChangeListener(PropertyChangeListener listener)
listener
- The PropertyChangeListener.public void addFTPListener(FTPListener listener)
listener
- The object listener.public OutputStream append(String fileName) throws IOException
fileName
- The file to put.
IOException
- If an error occurs while communicating with the server.public boolean append(String sourceFileName, String targetFileName) throws IOException
sourceFileName
- The file to put.targetFileName
- The file on the server.
IOException
- If an error occurs while communicating with the server.public boolean append(File sourceFileName, String targetFileName) throws IOException
sourceFileName
- The file to put.targetFileName
- The file on the server.
IOException
- If an error occurs while communicating with the server.public void addVetoableChangeListener(VetoableChangeListener listener)
listener
- The VetoableChangeListener.public boolean cd(String directory) throws IOException
directory
- The current directory to set on the server.
IOException
- If an error occurs while communicating with the server.public boolean connect() throws UnknownHostException, IOException, IllegalStateException
UnknownHostException
- If a path to the server cannot be found.
IOException
- If an error occurs while connecting to the server.
IllegalStateException
- If called before user and password are set.public String[] dir() throws IOException
IOException
- If an error occurs while communicating with the server.public String[] dir(String criteria) throws IOException
criteria
- The search criteria.
IOException
- If an error occurs while communicating with the server.public void disconnect() throws IOException
IOException
- If an error occurs while communicating with the server.protected void finalize() throws Throwable
Throwable
- If an error occurs during cleanup.public InputStream get(String fileName) throws IOException, FileNotFoundException
fileName
- The file to get.
IOException
- If an error occurs while communicating with the server.
FileNotFoundException
- If the name is a directory or the name is not found.public boolean get(String sourceFileName, String targetFileName) throws IOException, FileNotFoundException
sourceFileName
- The file to get on the server.targetFileName
- The file on the target file system.
IOException
- If an error occurs while communicating with the server.
FileNotFoundException
- If the source file or the targe file
cannot be accessed.public boolean get(String sourceFileName, File targetFile) throws IOException, FileNotFoundException
sourceFileName
- The file to get on the server.targetFile
- The file on the target file system.
IOException
- If an error occurs while communicating with the server.
FileNotFoundException
- If the source file or the targe file
cannot be accessed.public int getBufferSize()
public String getCurrentDirectory() throws IOException
IOException
- If an error occurs while communicating with the server.public String getLastMessage()
public int getMode()
public int getPort()
public String getServer()
public String getUser()
public boolean isReuseSocket()
setMode(int)
public String issueCommand(String cmd) throws IOException
The command is not altered before sending it to the server, so it
must 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.
Throws SecurityException if userid or password is invalid.
cmd
- The command to send to the server.
IOException
- If an error occurs while communicating with the server.public String[] ls() throws IOException
IOException
- If an error occurs while communicating with the server.public String[] ls(String criteria) throws IOException
criteria
- The search criteria.
IOException
- If an error occurs while communicating with the server.public boolean noop() throws IOException
IOException
- If an error occurs while communicating with the server.public OutputStream put(String fileName) throws IOException
fileName
- The file to put.
IOException
- If an error occurs while communicating with the server.public boolean put(String sourceFileName, String targetFileName) throws IOException
sourceFileName
- The file to put.targetFileName
- The file on the server.
IOException
- If an error occurs while communicating with the server.public boolean put(File sourceFileName, String targetFileName) throws IOException
sourceFileName
- The file to put.targetFileName
- The file on the server.
IOException
- If an error occurs while communicating with the server.public String pwd() throws IOException
IOException
- If an error occurs while communicating with the server.public void removePropertyChangeListener(PropertyChangeListener listener)
listener
- The PropertyChangeListener.public void removeFTPListener(FTPListener listener)
listener
- The FTP listener.public void removeVetoableChangeListener(VetoableChangeListener listener)
listener
- The VetoableChangeListener.public void setBufferSize(int bufferSize) throws PropertyVetoException
bufferSize
- The size of the buffer used when transferring files.
PropertyVetoException
- If the change is vetoed.public boolean setCurrentDirectory(String directory) throws IOException
directory
- The current directory to set on the server.
IOException
- If an error occurs while communicating with the server.public void setDataTransferType(int transferType) throws IOException
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 SecurityException if userid or password is invalid.
IOException
- If an error occurs while communicating with the server.public void setMode(int mode)
mode
- The mode. Valid values are ACTIVE_MODE
or PASSIVE_MODE
.public void setPassword(String password)
password
- The password for the user.public void setPort(int port) throws PropertyVetoException
port
- The port to use when connecting to the server.
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(String server) throws PropertyVetoException
server
- The name of the server to which this object connects.
PropertyVetoException
- If the change is vetoed.public void setUser(String user) throws PropertyVetoException
user
- The user identifier used when connecting to the server.
PropertyVetoException
- If the change is vetoed.
IllegalStateException
- If connection already established to the server.public int ren(String fromName, String toName) throws 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 ".".
IOException
- If an error occurs while communicating with the server.public static String generateNewName(String fromName, 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 ".".
NullPointerException
- if the fromName or toName
parameters are null.
IllegalArgumentException
- if fromName or toName
is null, or if the toName contains an invalid expression.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |