public class FtpBean
extends java.lang.Object
How to use it?
To start a new instance of this FTP bean
FtpBean ftp = new FtpBean();
// To connect to a FTP server
ftp.ftpConnect("servername", "username", "password");
// Some methods return null if you call them without establish connection first.
//Then just call the functions provided by this bean.
//After you end the ftp section. Just close the connection.
ftp.close();
Remarks:
1) Whenever a ftp command failed (eg. Permission denied), the methods throw an FtpException.
So you need to catch the FtpException wherever you invoke those methods of this class.
2) This bean use passive mode to establish data connection by default.
If this cause problem from firewall of the network, try using active mode:
ftp.setPassiveModeTransfer(false);
3) To turn on debug mode, you need to change the source of this class. Then re-compile it.
4) For timeout on creating Socket. if a timeout is being set and operation timeout, a
java.io.InterruptedIOException is throw. This is the case for both passive transfer mode and
establishment of connection to the server at the beginning. For active transfer mode, timeout
is set in the servers ftpd. If there is timeout, the servers ftp software return an error
code which causing the bean to throw a ftp.FtpException.
IMPORTANT for using in an Applet:
1) If you use this bean in an applet and the applet is open to the public,
please don't include the user name and password in
the source code of your applet. As anyone who can get your class files can get your
user name and password. It is reasonable to ask the user for user name and password
if you are going to use FTP in the applet.
2) If you use it in an applet, please be aware of the security restriction from the browser.
As an unsigned applet can ONLY connect to the host which serves it. Also, some methods in this bean
will write/read to the local file system. These methods are also restricted by the browser.
If you find any bugs in this bean or any comment, please give me a notice at
Calvin(calvin_tai2000@yahoo.com.hk)
Modifier and Type | Field and Description |
---|---|
protected static java.lang.String |
CMD_ACCT |
protected static java.lang.String |
CMD_APPE |
protected static java.lang.String |
CMD_CDUP |
protected static java.lang.String |
CMD_CWD |
protected static java.lang.String |
CMD_DELE |
protected static java.lang.String |
CMD_LIST |
protected static java.lang.String |
CMD_MKD |
protected static java.lang.String |
CMD_PASS |
protected static java.lang.String |
CMD_PASV |
protected static java.lang.String |
CMD_PORT |
protected static java.lang.String |
CMD_PWD |
protected static java.lang.String |
CMD_QUIT |
protected static java.lang.String |
CMD_REST |
protected static java.lang.String |
CMD_RETR |
protected static java.lang.String |
CMD_RMD |
protected static java.lang.String |
CMD_RNFR |
protected static java.lang.String |
CMD_RNTO |
protected static java.lang.String |
CMD_SITE |
protected static java.lang.String |
CMD_STOR |
protected static java.lang.String |
CMD_SYST |
protected static java.lang.String |
CMD_TYPE |
protected static java.lang.String |
CMD_USER |
protected static java.lang.String |
FTP_INIT |
protected static java.lang.String |
REPLY_PER_NEG |
protected static java.lang.String |
REPLY_POS_CMP |
protected static java.lang.String |
REPLY_POS_INT |
protected static java.lang.String |
REPLY_POS_PRE |
protected static java.lang.String |
REPLY_TRA_NEG |
protected static java.lang.String |
REPLY_UNDEF |
Constructor and Description |
---|
FtpBean()
Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
addPropertyChangeListener(java.beans.PropertyChangeListener listener) |
void |
appendAsciiFile(java.lang.String filename,
java.lang.String content,
java.lang.String separator)
Append an ascii file to the server.
|
void |
appendBinaryFile(java.lang.String filename)
Read a file from local hard disk and append a file on the server.
|
void |
appendBinaryFile(java.lang.String local_file,
java.lang.String remote_file)
Read a file from local hard disk and append to the server with restarting point.
|
void |
appendBinaryFile(java.lang.String local_file,
java.lang.String remote_file,
FtpObserver observer)
Read a file from local hard disk and append to the server with restarting point.
|
void |
close()
Close FTP connection.
|
void |
execute(java.lang.String exec)
Execute a command using ftp.
|
void |
fileDelete(java.lang.String filename)
Delete a file at the FTP server.
|
void |
fileRename(java.lang.String oldfilename,
java.lang.String newfilename)
Rename a file at the FTP server.
|
void |
ftpConnect(java.lang.String server,
java.lang.String user)
Connect to Ftp server and login.
|
void |
ftpConnect(java.lang.String server,
java.lang.String user,
java.lang.String password)
Connect to Ftp server and login.
|
void |
ftpConnect(java.lang.String server,
java.lang.String user,
java.lang.String password,
java.lang.String acct)
Connect to FTP server and login.
|
java.lang.String |
getAcctInfo()
Return the account information.
|
java.lang.String |
getAsciiFile(java.lang.String filename,
java.lang.String separator)
Get an ASCII file from the server and return as String.
|
java.lang.String |
getAsciiFile(java.lang.String filename,
java.lang.String separator,
FtpObserver observer)
Get an ASCII file from the server and return as String.
|
void |
getAsciiFile(java.lang.String ftpfile,
java.lang.String localfile,
java.lang.String separator)
Get an ascii file from the server and write to local file system.
|
void |
getAsciiFile(java.lang.String ftpfile,
java.lang.String localfile,
java.lang.String separator,
FtpObserver observer)
Get an ascii file from the server and write to local file system.
|
byte[] |
getBinaryFile(java.lang.String filename)
Get a binary file and return a byte array.
|
byte[] |
getBinaryFile(java.lang.String filename,
FtpObserver observer)
Get a binary file and return a byte array.
|
byte[] |
getBinaryFile(java.lang.String filename,
long restart)
Get a binary file at a restarting point.
|
byte[] |
getBinaryFile(java.lang.String filename,
long restart,
FtpObserver observer)
Get a binary file at a restarting point.
|
void |
getBinaryFile(java.lang.String ftpfile,
java.lang.String localfile)
Read file from ftp server and write to a file in local hard disk.
|
void |
getBinaryFile(java.lang.String ftpfile,
java.lang.String localfile,
FtpObserver observer)
Read file from ftp server and write to a file in local hard disk.
|
void |
getBinaryFile(java.lang.String ftpfile,
java.lang.String localfile,
long restart)
Read file from ftp server and write to a file in local hard disk.
|
void |
getBinaryFile(java.lang.String ftpfile,
java.lang.String localfile,
long restart,
FtpObserver observer)
Read from a ftp file and restart at a specific point.
|
java.lang.String |
getDirectory()
Get current directory name.
|
FtpListResult |
getDirectoryContent()
Get the content of current directory
|
java.lang.String |
getDirectoryContentAsString()
Get the content of current directory.
|
int |
getPort()
Return the port number
|
java.lang.String |
getReply()
Get reply of the last command.
|
java.lang.String |
getReplyMessage()
Get reply message of the last command.
|
java.lang.String |
getServerName()
Return the server name.
|
int |
getSocketTimeout()
Get timeout when creating socket.
|
java.lang.String |
getSystemType()
Get the type of operating system of the server.
|
java.lang.String |
getUserName()
Return the user name.
|
boolean |
isPassiveModeTransfer()
Return true if it is using passive transfer mode.
|
void |
makeDirectory(java.lang.String directory)
Make a directory in the server.
|
void |
putAsciiFile(java.lang.String filename,
java.lang.String content,
java.lang.String separator)
Save an ascii file to the server.
|
void |
putBinaryFile(java.lang.String filename,
byte[] content)
Put a binary file to the server from an array of byte.
|
void |
putBinaryFile(java.lang.String filename,
byte[] content,
long restart)
Put a binary file to the server from an array of byte with a restarting point
|
void |
putBinaryFile(java.lang.String local_file,
java.lang.String remote_file)
Read a file from local hard disk and write to the server.
|
void |
putBinaryFile(java.lang.String local_file,
java.lang.String remote_file,
FtpObserver observer)
Read a file from local hard disk and write to the server.
|
void |
putBinaryFile(java.lang.String local_file,
java.lang.String remote_file,
long restart)
Read a file from local hard disk and write to the server with restarting point.
|
void |
putBinaryFile(java.lang.String local_file,
java.lang.String remote_file,
long restart,
FtpObserver observer)
Read a file from local hard disk and write to the server with restarting point.
|
void |
removeDirectory(java.lang.String directory)
Remove a directory in the server
|
void |
removePropertyChangeListener(java.beans.PropertyChangeListener listener) |
void |
setDirectory(java.lang.String directory)
Change directory.
|
void |
setPassiveModeTransfer(boolean passive)
Set passive transfer mode.
|
void |
setPort(int port)
Set port number if the port number of ftp is not 21
|
void |
setSocketTimeout(int timeout)
Set timeout when creating a socket.
|
void |
toParentDirectory()
Change to parent directory.
|
protected static final java.lang.String FTP_INIT
protected static final java.lang.String CMD_ACCT
protected static final java.lang.String CMD_APPE
protected static final java.lang.String CMD_CWD
protected static final java.lang.String CMD_CDUP
protected static final java.lang.String CMD_DELE
protected static final java.lang.String CMD_MKD
protected static final java.lang.String CMD_PASV
protected static final java.lang.String CMD_PASS
protected static final java.lang.String CMD_PORT
protected static final java.lang.String CMD_PWD
protected static final java.lang.String CMD_QUIT
protected static final java.lang.String CMD_RMD
protected static final java.lang.String CMD_REST
protected static final java.lang.String CMD_RETR
protected static final java.lang.String CMD_RNTO
protected static final java.lang.String CMD_RNFR
protected static final java.lang.String CMD_SITE
protected static final java.lang.String CMD_STOR
protected static final java.lang.String CMD_SYST
protected static final java.lang.String CMD_USER
protected static final java.lang.String CMD_TYPE
protected static final java.lang.String CMD_LIST
protected static final java.lang.String REPLY_POS_PRE
protected static final java.lang.String REPLY_POS_CMP
protected static final java.lang.String REPLY_POS_INT
protected static final java.lang.String REPLY_TRA_NEG
protected static final java.lang.String REPLY_PER_NEG
protected static final java.lang.String REPLY_UNDEF
public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
public void ftpConnect(java.lang.String server, java.lang.String user) throws java.io.IOException, FtpException
server
- Name of serveruser
- User name for loginFtpException
- if a ftp error occur (eg. Login fail in this case).java.io.IOException
- if an I/O error occurpublic void ftpConnect(java.lang.String server, java.lang.String user, java.lang.String password) throws java.io.IOException, FtpException
server
- Name of serveruser
- User name for loginpassword
- Password for loginFtpException
- if a ftp error occur (eg. Login fail in this case).java.io.IOException
- if an I/O error occurpublic void ftpConnect(java.lang.String server, java.lang.String user, java.lang.String password, java.lang.String acct) throws java.io.IOException, FtpException
server
- Name of serveruser
- User name for loginpassword
- Password for loginacct
- account informationFtpException
- if a ftp error occur (eg. Login fail in this case).java.io.IOException
- if an I/O error occurpublic void close() throws java.io.IOException, FtpException
java.io.IOException
- if an I/O error occurFtpException
- if a ftp error occurpublic void fileDelete(java.lang.String filename) throws java.io.IOException, FtpException
filename
- Name of the file to be deleted.FtpException
- if a ftp error occur. (eg. no such file in this case)java.io.IOException
- if an I/O error occur.public void fileRename(java.lang.String oldfilename, java.lang.String newfilename) throws java.io.IOException, FtpException
oldfilename
- The name of the file to be renamednewfilename
- The new name of the fileFtpException
- if a ftp error occur. (eg. A file named the new file name already in this case.)java.io.IOException
- if an I/O error occur.public java.lang.String getAsciiFile(java.lang.String filename, java.lang.String separator) throws java.io.IOException, FtpException
filename
- Name of ASCII file to be getted.separator
- The line separator you want in the return String (eg. "\r\n", "\n", "\r")FtpException
- if a ftp error occur. (eg. no such file in this case)java.io.IOException
- if an I/O error occur.public java.lang.String getAsciiFile(java.lang.String filename, java.lang.String separator, FtpObserver observer) throws java.io.IOException, FtpException
filename
- Name of ASCII file to be getted.separator
- The line separator you want in the return String (eg. "\r\n", "\n", "\r").observer
- The observer of the downloading progressFtpException
- if a ftp error occur. (eg. no such file in this case)java.io.IOException
- if an I/O error occur.FtpObserver
public void getAsciiFile(java.lang.String ftpfile, java.lang.String localfile, java.lang.String separator) throws java.io.IOException, FtpException
ftpfile
- Name of ascii file in the server side.localfile
- Name of ascii file in the local file system.separator
- The line separator you want in the local ascii file (eg. "\r\n", "\n", "\r").FtpException
- if a ftp error occur. (eg. no such file in this case)java.io.IOException
- if an I/O error occur.public void getAsciiFile(java.lang.String ftpfile, java.lang.String localfile, java.lang.String separator, FtpObserver observer) throws java.io.IOException, FtpException
ftpfile
- Name of ascii file in the server side.localfile
- Name of ascii file in the local file system.separator
- The line separator you want in the local ascii file (eg. "\r\n", "\n", "\r").observer
- The observer of the downloading progress.FtpException
- if a ftp error occur. (eg. no such file in this case)java.io.IOException
- if an I/O error occur.FtpObserver
public void appendAsciiFile(java.lang.String filename, java.lang.String content, java.lang.String separator) throws java.io.IOException, FtpException
filename
- The name of filecontent
- The String content of the fileseparator
- Line separator of the contentFtpException
- if a ftp error occur. (eg. permission denied in this case)java.io.IOException
- if an I/O error occur.public void putAsciiFile(java.lang.String filename, java.lang.String content, java.lang.String separator) throws java.io.IOException, FtpException
filename
- The name of filecontent
- The String content of the fileseparator
- Line separator of the contentFtpException
- if a ftp error occur. (eg. permission denied in this case)java.io.IOException
- if an I/O error occur.public byte[] getBinaryFile(java.lang.String filename) throws java.io.IOException, FtpException
filename
- The name of the binary file to be got.FtpException
- if a ftp error occur. (eg. No such file in this case)java.io.IOException
- if an I/O error occur.public byte[] getBinaryFile(java.lang.String filename, FtpObserver observer) throws java.io.IOException, FtpException
filename
- The name of the binary file to be got.observer
- The observer of the downloading progress.FtpException
- if a ftp error occur. (eg. No such file in this case)java.io.IOException
- if an I/O error occur.public byte[] getBinaryFile(java.lang.String filename, long restart) throws java.io.IOException, FtpException
filename
- Name of binary file to be getted.restart
- Restarting point, ignored if less than or equal to zero.FtpException
- if a ftp error occur. (eg. No such file in this case)java.io.IOException
- if an I/O error occur.public byte[] getBinaryFile(java.lang.String filename, long restart, FtpObserver observer) throws java.io.IOException, FtpException
filename
- Name of binary file to be getted.restart
- Restarting point, ignored if less than or equal to zero.observer
- The FtpObserver which monitor this downloading progressFtpException
- if a ftp error occur. (eg. No such file in this case)java.io.IOException
- if an I/O error occur.FtpObserver
public void getBinaryFile(java.lang.String ftpfile, java.lang.String localfile) throws java.io.IOException, FtpException
ftpfile
- Name of file to be get from the ftp server, can be in full path.localfile
- Name of local file to be write, can be in full path.FtpException
- if a ftp error occur. (eg. No such file in this case)java.io.IOException
- if an I/O error occur.public void getBinaryFile(java.lang.String ftpfile, java.lang.String localfile, long restart) throws java.io.IOException, FtpException
ftpfile
- Name of file to be get from the ftp server, can be in full path.localfile
- Name of local file to be write, can be in full path.restart
- Restarting pointFtpException
- if a ftp error occur. (eg. No such file in this case)java.io.IOException
- if an I/O error occur.public void getBinaryFile(java.lang.String ftpfile, java.lang.String localfile, FtpObserver observer) throws java.io.IOException, FtpException
ftpfile
- Name of file to be get from the ftp server, can be in full path.localfile
- Name of local file to be write, can be in full path.observer
- The FtpObserver which monitor this downloading progressFtpException
- if a ftp error occur. (eg. No such file in this case)java.io.IOException
- if an I/O error occur.FtpObserver
public void getBinaryFile(java.lang.String ftpfile, java.lang.String localfile, long restart, FtpObserver observer) throws java.io.IOException, FtpException
ftpfile
- Name of file to be get from the ftp server, can be in full path.localfile
- File name of local filerestart
- Restarting point, ignored if equal or less than zero.observer
- The FtpObserver which monitor this downloading progressFtpException
- if a ftp error occur. (eg. No such file in this case)java.io.IOException
- if an I/O error occur.FtpObserver
public void putBinaryFile(java.lang.String filename, byte[] content) throws java.io.IOException, FtpException
filename
- The name of file.content
- The byte array to be written to the server.FtpException
- if a ftp error occur. (eg. permission denied in this case)java.io.IOException
- if an I/O error occur.public void putBinaryFile(java.lang.String filename, byte[] content, long restart) throws java.io.IOException, FtpException
filename
- The name of file.content
- The byte array to be write to the server.restart
- The restarting point, ingored if less than or equal to zero.FtpException
- if a ftp error occur. (eg. permission denied in this case)java.io.IOException
- if an I/O error occur.public void putBinaryFile(java.lang.String local_file, java.lang.String remote_file) throws java.io.IOException, FtpException
local_file
- Name of local file, can be in full path.remote_file
- Name of file in the ftp server, can be in full path.FtpException
- if a ftp error occur. (eg. permission denied)java.io.IOException
- if an I/O error occur.public void putBinaryFile(java.lang.String local_file, java.lang.String remote_file, FtpObserver observer) throws java.io.IOException, FtpException
local_file
- Name of local file, can be in full path.remote_file
- Name of file in the ftp server, can be in full path.observer
- The FtpObserver which monitor this uploading progress.FtpException
- if a ftp error occur. (eg. permission denied)java.io.IOException
- if an I/O error occur.public void putBinaryFile(java.lang.String local_file, java.lang.String remote_file, long restart) throws java.io.IOException, FtpException
local_file
- Name of local file, can be in full path.remote_file
- Name of file in the ftp server, can be in full path.restart
- The restarting point, ignored if less than or greater than zero.FtpException
- if a ftp error occur. (eg. permission denied)java.io.IOException
- if an I/O error occur.public void putBinaryFile(java.lang.String local_file, java.lang.String remote_file, long restart, FtpObserver observer) throws java.io.IOException, FtpException
local_file
- Name of local file, can be in full path.remote_file
- Name of file in the ftp server, can be in full path.observer
- The FtpObserver which monitor this uploading progressFtpException
- if a ftp error occur. (eg. permission denied)java.io.IOException
- if an I/O error occur.public void appendBinaryFile(java.lang.String filename) throws java.io.IOException, FtpException
filename
- Name of local file and remote file.FtpException
- if a ftp error occur. (eg. permission denied)java.io.IOException
- if an I/O error occur.public void appendBinaryFile(java.lang.String local_file, java.lang.String remote_file) throws java.io.IOException, FtpException
local_file
- Name of local file, can be in full path.remote_file
- Name of file in the ftp server, can be in full path.FtpException
- if a ftp error occur. (eg. permission denied)java.io.IOException
- if an I/O error occur.public void appendBinaryFile(java.lang.String local_file, java.lang.String remote_file, FtpObserver observer) throws java.io.IOException, FtpException
local_file
- Name of local file, can be in full path.remote_file
- Name of file in the ftp server, can be in full path.observer
- The FtpObserver which monitor this uploading progressFtpException
- if a ftp error occur. (eg. permission denied)java.io.IOException
- if an I/O error occur.public java.lang.String getDirectory() throws java.io.IOException, FtpException
FtpException
- if a ftp error occur.java.io.IOException
- if an I/O error occur.public void setDirectory(java.lang.String directory) throws java.io.IOException, FtpException
directory
- Name of directoryFtpException
- if a ftp error occur. (eg. permission denied in this case)java.io.IOException
- if an I/O error occur.public void toParentDirectory() throws java.io.IOException, FtpException
FtpException
- if a ftp error occur. (eg. permission denied in this case)java.io.IOException
- if an I/O error occur.public FtpListResult getDirectoryContent() throws java.io.IOException, FtpException
FtpException
- if a ftp error occur. (eg. permission denied in this case)java.io.IOException
- if an I/O error occur.FtpListResult
public java.lang.String getDirectoryContentAsString() throws java.io.IOException, FtpException
FtpException
- if a ftp error occur. (eg. permission denied in this case)java.io.IOException
- if an I/O error occur.public void makeDirectory(java.lang.String directory) throws java.io.IOException, FtpException
directory
- The name of directory to be made.FtpException
- if a ftp error occur. (eg. permission denied in this case)java.io.IOException
- if an I/O error occur.public void removeDirectory(java.lang.String directory) throws java.io.IOException, FtpException
directory
- The name of directory to be removedFtpException
- if a ftp error occur. (eg. permission denied in this case)java.io.IOException
- if an I/O error occur.public void execute(java.lang.String exec) throws java.io.IOException, FtpException
exec
- The command to execute.FtpException
- if a ftp error occur. (eg. command not understood)java.io.IOException
- if an I/O error occur.public java.lang.String getSystemType() throws java.io.IOException, FtpException
java.io.IOException
FtpException
public int getPort()
public void setPort(int port)
public void setSocketTimeout(int timeout) throws java.net.SocketException
timeout
- Timeout in milliseconds, 0 means infinity.java.net.SocketException
public int getSocketTimeout() throws java.net.SocketException
java.net.SocketException
public java.lang.String getAcctInfo()
public java.lang.String getServerName()
public java.lang.String getUserName()
public java.lang.String getReply()
public java.lang.String getReplyMessage()
public boolean isPassiveModeTransfer()
public void setPassiveModeTransfer(boolean passive)
passive
- Using passive transfer if true.