public class FTP
extends java.lang.Object
Note: if you're experiencing slowness in the server, try to disable the reverse-ip resolution in your FTP server: this will speedup everything.
Check the sample RemoteExplorer. Here is an example code:  ftp = new FTP(url, user, pass, cbLog);   ftp.makeDir("tempdir");   ftp.changeDir("tempdir");   String textfile = "Viva Verinha!";   ByteArrayStream bas = new ByteArrayStream(textfile.getBytes());   ftp.sendFile(bas, "verinha.txt", false);   String[] files = ftp.list("*.txt");   if (files == null || files.length != 1)   cbLog.add("Something went wrong in sending files...");   ftp.rename("verinha.txt", "vivaverinha.txt");   bas = new ByteArrayStream(50);   ftp.receiveFile("vivaverinha.txt", bas);   cbLog.add(new String(bas.getCopy()));   ftp.delete("vivaverinha.txt");   ftp.changeDir("..");   ftp.removeDir("tempdir");You can use the CompressedByteArrayStream to transfer and receive big files to/from the server (check the class for more information and samples).
To transfer a File to the server, all you have to do is:
  File f = new File("michelle.txt", File.READ_WRITE);   ftp.sendFile(f, "michelle.txt", false); // last parameter depends on what you want to doIt is currently impossible to transfer a whole PDBFile to/from the server. The solution for this would be to send each record in pieces, and store it in separate files in the server. Then a routine written in TotalCross in the server could reassemble the records into a new PDBFile. Here is an idea:
  // for the Client:   // i assume that a ftp class is prepared to be used   String name = "myPDBFile";   PDBFile cat = new PDBFile(name + ".crtr.type", READ_WRITE);   int n = cat.getRecordCount();   for (int i = 0; i < n; i++)   {   if (!cat.setRecordPos(i))   throw new RuntimeException("PDBFile is in use elsewhere!");   else   ftp.sendFile(cat, name + "#" + i, false);   }   // for the server   String name = "myPDBFile";   // for simplicity, I'll assume that the PDBFile does not exists   byte[] buf = new byte[65536]; // in desktop this is possible   PDBFile cat = new PDBFile(name + ".crtr.type", PDBFile.CREATE);   for (int i = 0;; i++)   {   File f = new File(name + "#" + i, File.READ_WRITE);   if (!f.exists())   break; // no more records   int size = f.getSize();   f.readBytes(buf, 0, size);   f.delete(); // could be also: f.close();   cat.addRecord(size);   cat.writeBytes(buf, 0, size);   }   cat.close();The example above can be easily changed to add support for compression.
Here is a list of error codes that can thrown if an Exception occurs:
Code | Description |
100 Codes | The requested action is being taken. Expect a reply before proceeding with a new command. |
110 | Restart marker reply. |
120 | Service ready in (n) minutes. |
125 | Data connection already open, transfer starting. |
150 | File status okay, about to open data connection. |
200 Codes | The requested action has been successfully completed. |
200 | Command okay. |
202 | Command not implemented |
211 | System status, or system help reply. |
212 | Directory status. |
213 | File status. |
214 | Help message. |
215 | NAME system type. (NAME is an official system name from the list in the Assigned Numbers document.) |
220 | Service ready for new user. |
221 | Service closing control connection. (Logged out if appropriate.) |
225 | Data connection open, no transfer in progress. |
226 | Closing data connection. Requested file action successful (file transfer, abort, etc.). |
227 | Entering Passive Mode |
230 | User logged in, proceed. |
250 | Requested file action okay, completed. |
257 | "PATHNAME" created. |
300 Codes | The command has been accepted, but the requested action is being held pending receipt of further information. |
331 | User name okay, need password. |
332 | Need account for login. |
350 | Requested file action pending further information. If you're using "list *.txt", try "list *.*" and filter localy. |
400 Codes | The command was not accepted and the requested action
did not take place. Tthe error condition is temporary, however, and the action may be requested again. |
421 | Service not available, closing control connection. (May be a reply to any command if the service knows it must shut down.)' |
425 | Can't open data connection. |
426 | Connection closed, transfer aborted. |
450 | Requested file action not taken. File unavailable (e.g., file busy). |
451 | Requested action aborted, local error in processing. |
452 | Requested action not taken. Insufficient storage space in system. |
500 Codes | The command was not accepted and the requested action did not take place. |
500 | Syntax error, command unrecognized. This may include errors such as command line too long. |
501 | Syntax error in parameters or arguments. |
502 | Command not implemented. |
503 | Bad sequence of commands. |
504 | Command not implemented for that parameter. |
530 | User not logged in. |
532 | Need account for storing files. |
550 | Requested action not taken. File unavailable (e.g., file not found, no access). |
552 | Requested file action aborted, storage allocation exceeded |
553 | Requested action not taken. Illegal file name. |
Modifier and Type | Class and Description |
---|---|
static interface |
FTP.ProgressInformation
Assign the progressInfo member to an instance of this interface to receive information of how many bytes were transfered.
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
ASCII
Defines an ASCII transfer type.
|
static java.lang.String |
BINARY
Defines a BINARY transfer type.
|
static int |
defaultOpenTimeout
Default open timeout: 15 seconds.
|
static int |
defaultPort
Default port: 21.
|
static int |
defaultReadTimeout
Default read timeout: 5 seconds.
|
static int |
defaultWriteTimeout
Default write timeout: 5 seconds.
|
static java.lang.String |
EBCDIC
Defines an EBCDIC transfer type.
|
static boolean |
log2console
Set this to true to send the log to both the console and the combo.
|
FTP.ProgressInformation |
progressInfo
Assign the ProgressInformation member to receive information about the file transfer.
|
int |
sendSleep
This makes a sleep during the send of a file.
|
Constructor and Description |
---|
FTP(java.lang.String url,
java.lang.String user,
java.lang.String pass)
Opens a socket to the given URL, user, password and the default timeouts.
|
FTP(java.lang.String url,
java.lang.String user,
java.lang.String pass,
Control loggingControl)
Opens a socket to the given URL, user, password, logging control and the default timeouts.
|
FTP(java.lang.String url,
java.lang.String user,
java.lang.String pass,
int port)
Opens a socket to the given URL, user, password, port and the default timeouts.
|
FTP(java.lang.String url,
java.lang.String user,
java.lang.String pass,
int openTimeout,
int readTimeout,
int writeTimeout)
Opens a socket to the given URL, user, password, and timeouts.
|
FTP(java.lang.String url,
java.lang.String user,
java.lang.String pass,
int openTimeout,
int readTimeout,
int writeTimeout,
Control loggingControl)
Opens a socket to the given URL, user, password, timeouts and logging control
|
FTP(java.lang.String url,
java.lang.String user,
java.lang.String pass,
int port,
int openTimeout,
int readTimeout,
int writeTimeout,
Control loggingControl)
Opens a socket to the given URL, user, password, port, timeouts and logging control.
|
Modifier and Type | Method and Description |
---|---|
void |
createDir(java.lang.String pathName)
Creates a directory at the server site.
|
void |
delete(java.lang.String pathName)
Deletes the file specified at the server site.
|
void |
forceClose()
Closes the control connection without issuing any quit or abort command to the server.
|
java.lang.String |
getCurrentDir()
Returns the name of the current working directory.
|
java.lang.String[] |
list(java.lang.String pathName)
List the contents on the specified pathname.
|
java.lang.String[] |
listNames(java.lang.String pathName)
List the names of the files on the specified pathname.
|
void |
noop()
This command may help to keep the connection open.
|
void |
quit()
Sends a quit command to the server and closes the socket.
|
int |
receiveFile(java.lang.String pathName,
Stream outputStream)
Transfers a copy of the file specified in the pathname from the server.
|
void |
removeDir(java.lang.String pathName)
Causes the directory specified in the pathname to be removed as a directory (if the pathname is absolute) or as a
subdirectory of the current working directory (if the pathname is relative).
|
void |
rename(java.lang.String oldPathName,
java.lang.String newPathName)
Renames a file at the server site.
|
int |
sendFile(Stream inputStream,
java.lang.String pathName)
Sends a file to be stored at the server site.
|
void |
setCurrentDir(java.lang.String pathName)
Changes the user's current working directory to the given one.
|
void |
setLoggingControl(Control c)
Sets the control where the log will be displayed.
|
void |
setPort(java.lang.String port)
The argument is a HOST-PORT specification for the data port to be used in data connection.
|
void |
setType(java.lang.String type)
Sets the data representation type.
|
public static final int defaultPort
public static final int defaultOpenTimeout
public static final int defaultReadTimeout
public static final int defaultWriteTimeout
public static final java.lang.String ASCII
setType(String)
,
Constant Field Valuespublic static final java.lang.String BINARY
setType(String)
,
Constant Field Valuespublic static final java.lang.String EBCDIC
setType(String)
,
Constant Field Valuespublic FTP.ProgressInformation progressInfo
public int sendSleep
public static boolean log2console
public FTP(java.lang.String url, java.lang.String user, java.lang.String pass) throws UnknownHostException, IOException, FTPConnectionClosedException
url
- The url as an ip or a full address to the server. The connection port is always 21user
- The user name for loginpass
- The password for loginUnknownHostException
IOException
FTPConnectionClosedException
FTP(String, String, String, int, int, int, int, Control)
public FTP(java.lang.String url, java.lang.String user, java.lang.String pass, int port) throws UnknownHostException, IOException, FTPConnectionClosedException
url
- The url as an ip or a full address to the server.user
- The user name for loginpass
- The password for loginport
- The port used to open the connection.UnknownHostException
IOException
FTPConnectionClosedException
FTP(String, String, String, int, int, int, int, Control)
public FTP(java.lang.String url, java.lang.String user, java.lang.String pass, Control loggingControl) throws UnknownHostException, IOException, FTPConnectionClosedException
url
- The url as an ip or a full address to the server. The connection port is always 21user
- The user name for loginpass
- The password for loginloggingControl
- The ListBox or ComboBox where the logging will be sent to.UnknownHostException
IOException
FTPConnectionClosedException
FTP(String, String, String, int, int, int, int, Control)
public FTP(java.lang.String url, java.lang.String user, java.lang.String pass, int openTimeout, int readTimeout, int writeTimeout) throws UnknownHostException, IOException, FTPConnectionClosedException
url
- The url as an ip or a full address to the server. The connection port is always 21user
- The user name for loginpass
- The password for loginopenTimeout
- The timeout used for the socket openreadTimeout
- The timeout used for read operationswriteTimeout
- The timeout used for write operationsUnknownHostException
IOException
FTPConnectionClosedException
FTP(String, String, String, int, int, int, int, Control)
public FTP(java.lang.String url, java.lang.String user, java.lang.String pass, int openTimeout, int readTimeout, int writeTimeout, Control loggingControl) throws UnknownHostException, IOException, FTPConnectionClosedException
url
- The url as an ip or a full address to the server. The connection port is always 21user
- The user name for loginpass
- The password for loginopenTimeout
- The timeout used for the socket openreadTimeout
- The timeout used for read operationswriteTimeout
- The timeout used for write operationsloggingControl
- The ListBox or ComboBox where the logging will be sent to.UnknownHostException
IOException
FTPConnectionClosedException
FTP(String, String, String, int, int, int, int, Control)
public FTP(java.lang.String url, java.lang.String user, java.lang.String pass, int port, int openTimeout, int readTimeout, int writeTimeout, Control loggingControl) throws UnknownHostException, IOException, FTPConnectionClosedException
url
- The url as an ip or a full address to the server.user
- The user name for loginpass
- The password for loginport
- The port used to open the connection.openTimeout
- The timeout used for the socket openreadTimeout
- The timeout used for read operationswriteTimeout
- The timeout used for write operationsloggingControl
- The ListBox or ComboBox where the logging will be sent to.UnknownHostException
IOException
FTPConnectionClosedException
public void quit() throws FTPConnectionClosedException, IOException
public java.lang.String getCurrentDir() throws FTPConnectionClosedException, IOException
FTPConnectionClosedException
IOException
public void setCurrentDir(java.lang.String pathName) throws FTPConnectionClosedException, IOException
pathName
- Specifies a directory or other system dependent file group designator.FTPConnectionClosedException
IOException
public void setType(java.lang.String type) throws FTPConnectionClosedException, IOException
type
- specifies the data representation type.java.lang.NullPointerException
- if type is null.FTPConnectionClosedException
IOException
ASCII
,
BINARY
,
EBCDIC
public void setPort(java.lang.String port) throws FTPConnectionClosedException, IOException
PORT h1,h2,h3,h4,p1,p2where h1 is the high order 8 bits of the internet host address.
port
- port to be used in data connectionjava.lang.NullPointerException
- if port is null.FTPConnectionClosedException
IOException
public void delete(java.lang.String pathName) throws FTPConnectionClosedException, IOException
pathName
- the file to be deleted.FTPConnectionClosedException
IOException
java.lang.NullPointerException
- if pathName is null.public void createDir(java.lang.String pathName) throws FTPConnectionClosedException, IOException
pathName
- Specifies the directory to be created.java.lang.NullPointerException
- if pathName is null.FTPConnectionClosedException
IOException
public void removeDir(java.lang.String pathName) throws FTPConnectionClosedException, IOException
pathName
- Specifies the directory to be removed.java.lang.NullPointerException
- if pathName is null.FTPConnectionClosedException
IOException
public void noop() throws FTPConnectionClosedException, IOException
public void rename(java.lang.String oldPathName, java.lang.String newPathName) throws FTPConnectionClosedException, IOException
oldPathName
- Old pathname of the file which is to be renamed.newPathName
- New pathname of the file specified to be renamed.java.lang.NullPointerException
- if any of the arguments, oldPathName and newPathName, is null.FTPConnectionClosedException
IOException
public int sendFile(Stream inputStream, java.lang.String pathName) throws FTPConnectionClosedException, IOException
inputStream
- The Stream from where the data will be read (using readBytes method)pathName
- The name of the destination file in the serverjava.lang.NullPointerException
- if any of the arguments, inputStream or pathName, is null.FTPConnectionClosedException
IOException
public int receiveFile(java.lang.String pathName, Stream outputStream) throws FTPConnectionClosedException, IOException
pathName
- Specifies the file to be retrieved.outputStream
- The stream to where the file will be written.java.lang.NullPointerException
- if any of the arguments, pathName or outputStream, is null.FTPConnectionClosedException
IOException
public java.lang.String[] list(java.lang.String pathName) throws FTPConnectionClosedException, IOException
pathName
- Specifies a directory, a group of files or a single file. A null value lists the user's current
directory or the default directory.FTPConnectionClosedException
IOException
public java.lang.String[] listNames(java.lang.String pathName) throws FTPConnectionClosedException, IOException
pathName
- Specifies a directory or a file group descriptor. A null value implies the user's current directory.FTPConnectionClosedException
IOException
public void setLoggingControl(Control c)
public void forceClose() throws IOException
IOException