|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objecttribble.net.ftp.FTPClientAdapter
tribble.net.ftp.FTPClient
public class FTPClient
Simple FTP client. Allows clients to establish FTP connections, send and receive files, get remote directory listings, etc.
This is a simple, bare-bones, no-nonsense implementation, providing only the most basic FTP capabilities, and performing only minimal error checking and recovery. If your FTP server is well behaved, though, this implementation should meet the basic needs of simple FTP applications.
Usage
This is a simple program that connects to an FTP server, uploads a file, downloads a file, then closes the connection to the server:
import tribble.net.ftp.*;
public class MyFtpClient
{
public static void main(String[] args)
throws Exception
{
FTPClientI ftp = null;
try
{
// Connect and login to the FTP server
ftp = new FTPClient();
ftp.setHost("ftp.domain.net");
ftp.connect();
ftp.login("userid", "password");
// Upload (put) a text file
ftp.setTextMode(true);
ftp.setRemoteDir("incoming");
ftp.putFile("file.txt", "file.txt");
// Download (get) a binary file
ftp.setRemoteDirUp();
ftp.setRemoteDir("outgoing");
ftp.setTextMode(false);
ftp.getFile("file.dat", "file.dat");
}
catch (FTPException ex)
{
// An error occurred
System.out.println(ex.getMessage());
throw ex;
}
finally
{
// Close the FTP connection
if (ftp != null)
ftp.disconnect();
}
}
}
References
IETF RFC 959 - File Transfer Protocol (FTP)
www.ietf.org/rfc/rfc0959.txt.
Copyright ©2001-2010 by David R. Tribble, all rights reserved.
Permission is granted to any person or entity except those designated
by the United States Department of State as a terrorist, or terrorist
government or agency, to use and distribute this source code provided
that the original copyright notice remains present and unaltered.
| Field Summary |
|---|
| Fields inherited from class tribble.net.ftp.FTPClientAdapter |
|---|
m_bufSize, m_cmdPort, m_dataPort, m_debugOut, m_hostName, m_inBytes, m_isConnected, m_isLoggedOn, m_localDir, m_outBytes, m_passiveMode, m_password, m_remoteDir, m_stop, m_textMode, m_timeOut, m_userID |
| Constructor Summary | |
|---|---|
FTPClient()
Default constructor. |
|
| Method Summary | |
|---|---|
void |
appendFile(java.io.InputStream in,
java.lang.String dst)
Append (send) a file from the local system to a file on the remote FTP system. |
void |
connect()
Connect to the remote FTP system. |
void |
createDirectory(java.lang.String dir)
Create a directory on the remote FTP system. |
void |
disconnect()
Disconnect from the remote FTP system. |
void |
getDirectoryList(java.lang.String path,
int max,
java.io.OutputStream out)
Get (receive) a directory listing from the remote FTP system. |
java.util.ArrayList<java.lang.String> |
getDirectoryNames(java.lang.String path,
java.io.FilenameFilter filt,
int max)
Get (receive) a list of filenames in a directory on the remote FTP system. |
void |
getDirectoryNames(java.lang.String path,
int max,
java.io.OutputStream out)
Get (receive) a list of filenames in a directory on the remote FTP system. |
void |
getFile(java.lang.String src,
java.io.OutputStream out)
Get (receive) a file from the remote FTP system to the local system. |
void |
getHelp(java.io.OutputStream out)
Get (receive) a help listing of supported FTP commands from the remote FTP system. |
java.lang.String |
getRemoteDir()
Retrieve the current working directory of the remote FTP system. |
void |
getStatus(java.io.OutputStream out)
Retrieve the current status of the remote FTP system. |
void |
getSystemInfo(java.io.OutputStream out)
Retrieve the identity information of the remote FTP system. |
void |
login(java.lang.String user,
java.lang.String pwd)
Log on to the remote FTP system. |
static void |
main(java.lang.String[] args)
Test driver. |
void |
ping()
Ping the remote FTP system. |
void |
putFile(java.io.File src,
java.lang.String dst)
Put (send) a file from the local system to the remote FTP system. |
void |
putFile(java.io.InputStream in,
java.lang.String dst)
Put (send) a file from the local system to the remote FTP system. |
void |
removeDirectory(java.lang.String dir)
Remove a directory on the remote FTP system. |
void |
removeFile(java.lang.String file)
Remove a file on the remote FTP system. |
void |
rename(java.lang.String from,
java.lang.String to)
Rename a file or directory on the remote FTP system. |
boolean |
setAsciiMode(boolean flag)
Deprecated. (since 1.26, 2007-07-26) Use setTextMode() instead. |
java.lang.String |
setRemoteDir(java.lang.String dir)
Set the working directory on the remote FTP system. |
java.lang.String |
setRemoteDirUp()
Set the working directory on the remote FTP system to the parent directory of the current working directory. |
boolean |
setTextMode(boolean flag)
Set the transfer mode to text (ASCII) or binary. |
| Methods inherited from class tribble.net.ftp.FTPClientAdapter |
|---|
appendFile, appendFile, finalize, getCommandPort, getDataPort, getDirectoryList, getDirectoryNames, getDirectoryNames, getDirectoryNames, getFile, getFile, getHost, getInputByteCount, getLocalDir, getOutputByteCount, getPassword, getTimeOut, getUserID, isAbsoluteFilename, isConnected, isLoggedIn, login, putFile, resetStop, setBufferSize, setCommandPort, setDataPort, setHost, setLocalDir, setPassive, setPassword, setTimeOut, setUserID, stop |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Methods inherited from interface tribble.net.ftp.FTPClientI |
|---|
appendFile, getDirectoryList, getDirectoryNames, getFile, putFile |
| Methods inherited from interface tribble.net.ftp.FTPSimpleClientI |
|---|
appendFile, getCommandPort, getDataPort, getDirectoryNames, getFile, getHost, getLocalDir, getPassword, getTimeOut, getUserID, isConnected, isLoggedIn, login, setBufferSize, setCommandPort, setDataPort, setHost, setLocalDir, setPassive, setPassword, setTimeOut, setUserID |
| Constructor Detail |
|---|
public FTPClient()
| Method Detail |
|---|
public static void main(java.lang.String[] args)
throws java.lang.Exception
Usage
java tribble.net.ftp.FTPClient host port|- user|- password [-action...]
args - Command line arguments.
java.lang.ExceptionFTPClientRun
public void connect()
throws java.io.IOException
connect in interface FTPSimpleClientIconnect in class FTPClientAdapterjava.io.IOException - Thrown if unable to connect to the remote FTP system.disconnect()public void disconnect()
IOException.
disconnect in interface FTPSimpleClientIdisconnect in class FTPClientAdapterconnect()
public void login(java.lang.String user,
java.lang.String pwd)
throws java.io.IOException
login in interface FTPClientIlogin in class FTPClientAdapteruser - FTP user-ID for the remote system.pwd - User password. This can be empty ("").
java.io.IOException - Thrown if an error occurs.login(),
connect()public boolean setTextMode(boolean flag)
setTextMode in interface FTPSimpleClientIsetTextMode in class FTPClientAdapterflag - If true, the transfer mode is set to text (ASCII), otherwise it is set
to binary.
public boolean setAsciiMode(boolean flag)
setTextMode() instead.
flag - If true, the transfer mode is set to text (ASCII), otherwise it is set
to binary.
public void ping()
throws java.io.IOException
ping in interface FTPClientIping in class FTPClientAdapterjava.io.IOException - Thrown if an error occurs, e.g., the FTP connection is broken.
public void getSystemInfo(java.io.OutputStream out)
throws java.io.IOException
getSystemInfo in interface FTPClientIgetSystemInfo in class FTPClientAdapterout - Output stream to which the identification information is to be written.
java.io.IOException - Thrown if an I/O error occurs.
FTPStoppedException - Thrown if stop() is called, which terminates the transfer
(write) operation prematurely.
public void getStatus(java.io.OutputStream out)
throws java.io.IOException
getStatus in interface FTPClientIgetStatus in class FTPClientAdapterout - Output stream to which the status is to be written.
java.io.IOException - Thrown if an I/O error occurs.
FTPStoppedException - Thrown if stop() is called, which terminates the transfer
(write) operation prematurely.
public void getHelp(java.io.OutputStream out)
throws java.io.IOException
getHelp in interface FTPClientIgetHelp in class FTPClientAdapterout - Output stream to which the help listing is to be written.
java.io.IOException - Thrown if an error occurs.
FTPStoppedException - Thrown if stop() is called, which terminates the transfer
(write) operation prematurely.
public java.lang.String setRemoteDir(java.lang.String dir)
throws java.io.IOException
setRemoteDir in interface FTPSimpleClientIsetRemoteDir in class FTPClientAdapterdir - Remote directory name.
java.io.IOException - Thrown if an error occurs.setRemoteDirUp(),
getRemoteDir(),
setLocalDir()
public java.lang.String setRemoteDirUp()
throws java.io.IOException
setRemoteDirUp in interface FTPSimpleClientIsetRemoteDirUp in class FTPClientAdapterjava.io.IOException - Thrown if an error occurs.getRemoteDir(),
setRemoteDir(),
setLocalDir()
public java.lang.String getRemoteDir()
throws java.io.IOException
getRemoteDir in interface FTPSimpleClientIgetRemoteDir in class FTPClientAdapterjava.io.IOException - Thrown if an error occurs.setRemoteDir(),
setRemoteDirUp(),
getLocalDir()
public void getFile(java.lang.String src,
java.io.OutputStream out)
throws java.io.IOException
getFile in interface FTPSimpleClientIgetFile in class FTPClientAdaptersrc - Remote source filename. If this does not contain a directory prefix, the
current remote working directory is assumed.out - Output stream to write the contents of the file retrieved from the remote
FTP system to. Note that this stream is flushed but is not closed
after the contents have been transmitted.
java.io.IOException - Thrown if the file could not be transmitted or if any other error occurs.
FTPStoppedException - Thrown if stop() is called, which terminates the transfer
operation prematurely.getFile(),
getFile(),
putFile()
public void putFile(java.io.File src,
java.lang.String dst)
throws java.io.IOException
putFile in interface FTPSimpleClientIputFile in class FTPClientAdaptersrc - Local source filename. If this does not contain a directory prefix, the
current local working directory is assumed.dst - Remote target filename. If this does not contain a directory prefix, the
current remote working directory is assumed. This may be null, in which
case the base filename of src (without the directory prefix) is
used.
java.io.IOException - Thrown if the file could not be transmitted or if any other error occurs.putFile(),
putFile(),
getFile()
public void putFile(java.io.InputStream in,
java.lang.String dst)
throws java.io.IOException
putFile in interface FTPSimpleClientIputFile in class FTPClientAdapterin - Input stream containing the contents of the file to send to the remote FTP
system. Note that this stream is not closed after the contents
have been transmitted.dst - Remote target filename. If this does not contain a directory prefix, the
current remote working directory is assumed. This may be null, in which
case the base filename of src (without the directory prefix) is
used.
java.io.IOException - Thrown if the file could not be transmitted or if any other error occurs.
FTPStoppedException - Thrown if stop() is called, which terminates the transfer
operation prematurely.putFile(),
putFile(),
appendFile(),
getFile()
public void appendFile(java.io.InputStream in,
java.lang.String dst)
throws java.io.IOException
appendFile in interface FTPSimpleClientIappendFile in class FTPClientAdapterin - Input stream containing the contents of the file to send to the remote FTP
system. Note that this stream is not closed after the contents
have been transmitted.dst - Remote target filename. If this does not contain a directory prefix, the
current remote working directory is assumed. This may be null, in which
case the base filename of src (without the directory prefix) is
used.
java.io.IOException - Thrown if the file could not be transmitted or if any other error occurs.
FTPStoppedException - Thrown if stop() is called, which terminates the transfer
operation prematurely.appendFile(),
appendFile(),
putFile()
public void getDirectoryList(java.lang.String path,
int max,
java.io.OutputStream out)
throws java.io.IOException
getDirectoryList in interface FTPClientIgetDirectoryList in class FTPClientAdapterpath - The remote directory or filename to list. If this is empty
(""), the current remote working directory is assumed.max - Maximum number of filenames (output lines) to list. A value of zero (0)
specifies that there is no maximum.out - Output stream to which the directory listing is to be written.
java.io.IOException - Thrown if an error occurs.
FTPStoppedException - Thrown if stop() is called, which terminates the listing
(output) operation prematurely.
public void getDirectoryNames(java.lang.String path,
int max,
java.io.OutputStream out)
throws java.io.IOException
getDirectoryNames in interface FTPClientIgetDirectoryNames in class FTPClientAdapterpath - The remote directory or filename to list. If this is empty
(""), the current remote working directory is assumed.max - Maximum number of filenames to get. A value of zero (0) specifies that
there is no maximum.out - Output stream to which the directory listing is to be written.
java.io.IOException - Thrown if an error occurs.
FTPStoppedException - Thrown if stop() is called, which terminates the listing
(output) operation prematurely.
public java.util.ArrayList<java.lang.String> getDirectoryNames(java.lang.String path,
java.io.FilenameFilter filt,
int max)
throws java.io.IOException
getDirectoryNames in interface FTPSimpleClientIpath - The remote directory or filename to list. If this is empty
(""), the current remote working directory is assumed.filt - Filer to apply to the filenames. Only filenames that are accepted by the
filter will appear in the returned vector. If this is null, no filtering
is applied to the filenames. This object's accept() method is
called for each filename in the directory, being passed a null directory
(first argument) and the found filename (second argument).max - Maximum number of filenames to list. A value of zero (0) specifies that
there is no maximum.
java.io.IOException - Thrown if an error occurs.
FTPStoppedException - Thrown if stop() is called, which terminates the listing
operation prematurely.
public void rename(java.lang.String from,
java.lang.String to)
throws java.io.IOException
rename in interface FTPSimpleClientIrename in class FTPClientAdapterfrom - Old (existing) remote file or directory name to rename.to - New name to rename the remote file or directory to.
java.io.IOException - Thrown if an error occurs.
public void removeFile(java.lang.String file)
throws java.io.IOException
removeFile in interface FTPSimpleClientIremoveFile in class FTPClientAdapterfile - Name of the file to delete. If this specifies a relative filename, the
file is assumed to be located in the current remote working directory.
java.io.IOException - Thrown if an error occurs.
public void createDirectory(java.lang.String dir)
throws java.io.IOException
createDirectory in interface FTPSimpleClientIcreateDirectory in class FTPClientAdapterdir - Name of the directory to create. If this specifies a relative directory
name, the directory is created in the current remote working directory.
java.io.IOException - Thrown if an error occurs.
public void removeDirectory(java.lang.String dir)
throws java.io.IOException
removeDirectory in interface FTPSimpleClientIremoveDirectory in class FTPClientAdapterdir - Name of the directory to delete. If this specifies a relative directory
name, the directory is assumed to be located in the current remote working
directory.
java.io.IOException - Thrown if an error occurs.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||