superwaba.ext.xplat.io
Class FTP

java.lang.Object
  |
  +--waba.io.Stream
        |
        +--waba.io.Socket
              |
              +--superwaba.ext.xplat.io.FTP

public class FTP
extends Socket

This class implements the File Transfer Protocol.

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.mkDir("tempdir");
ftp.chgDir("tempdir");
String textfile = "Viva Verinha!";
ByteArrayStream bas = new ByteArrayStream(textfile.getBytes());
ftp.sendFile(bas, "verinha.txt", false);
String []files = ftp.list("*.txt", false);
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.chgDir("..");
ftp.rmDir("tempdir");
 
 
You can use the BigByteArrayStream 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_ONLY);
 ftp.sendFile(f, "michelle.txt", false); // last parameter depends on what you want to do
 
It is currently impossible to transfer a whole Catalog 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 SW in the server could reassemble the records into a new Catalog. Here is an idea:
 // for the Client:
 // i assume that a ftp class is prepared to be used
 String name = "mycatalog";
 Catalog cat = new Catalog(name+".crtr.type",READ_WRITE);
 if (!cat.isOpen()) return;
 int n = cat.getRecordCount();
 for (int i =0; i < n; i++)
 {
    if (!cat.setRecordPos(i))
       throw new RuntimeException("catalog is in use elsewhere!");
    else
       ftp.sendFile(cat, name+"#"+i, false);
 }
       
 // for the server
 String name = "mycatalog";
 // for simplicity, i'll assume that the catalog does not exists
 byte []buf = new byte[65536]; // in desktop this is possible
 Catalog cat = new Catalog(name+".crtr.type",Catalog.CREATE); 
 for (int i =0; ; i++)
 {
    File f = new File(name+"#"+i, File.READ_ONLY);
    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.

Since:
SuperWaba 5.6

Field Summary
static boolean log2console
          Set this to true to send the log to both the console and the combo.
 int sendSleep
          This makes a sleep during the send of a file.
 
Fields inherited from class waba.io.Socket
lastError, lastErrorStr, refreshBeforeEachRead
 
Constructor Summary
FTP(java.lang.String url, java.lang.String user, java.lang.String pass)
          Opens a socket to the given URL, using a connection timeout of 15 seconds and a read timeout of 5 seconds, logs in with the user/pass.
FTP(java.lang.String url, java.lang.String user, java.lang.String pass, Control loggingControl)
          Opens a socket to the given URL, using a connection timeout of 15 seconds and a read timeout of 5 seconds, logs in with the user/pass.
FTP(java.lang.String url, java.lang.String user, java.lang.String pass, int port)
          Opens a socket to the given URL, using a connection timeout of 15 seconds and a read timeout of 5 seconds, logs in with the user/pass.
FTP(java.lang.String url, java.lang.String user, java.lang.String pass, int openTimeout, int readTimeout)
          Opens a socket to the given URL, logs in with the user/pass.
FTP(java.lang.String url, java.lang.String user, java.lang.String pass, int openTimeout, int readTimeout, Control loggingControl)
          Opens a socket to the given URL, logs in with the user/pass.
FTP(java.lang.String url, java.lang.String user, java.lang.String pass, int port, int openTimeout, int readTimeout, Control loggingControl)
          Opens a socket to the given URL, logs in with the user/pass, at the given port.
 
Method Summary
 void chgDir(java.lang.String name)
          Changes the current directory to the given one
 void delete(java.lang.String name)
          Deletes the given file
 java.lang.String getCurrentDir()
          Returns the current directory of the server.
 java.lang.String[] list(java.lang.String wildCard, boolean includeFolders)
          List the contents of the current directory.
 void mkDir(java.lang.String name)
          Creates a directory at the server
 void noop()
          This command helps to keep the connection open.
 void quit()
          Sends a quit command to the server and close this socket.
 int receiveFile(java.lang.String name, Stream outputStream)
          Retrieves a file from the server.
 void rename(java.lang.String nameSrc, java.lang.String nameDest)
          Renames a file
 void rmDir(java.lang.String name)
          Removes a directory at the server
 int sendFile(Stream inputStream, java.lang.String name)
          Sends a file to the server.
 void setLoggingControl(Control c)
          Sets the control where the log will be displayed.
 void setPort(java.lang.String port)
          Issues the command PORT
 void setType(java.lang.String type)
          Changes the type to ASCII or BINARY
 
Methods inherited from class waba.io.Socket
close, disconnect, finalize, getStatus, isOpen, readBytes, readBytes, readLine, setReadTimeout, writeBytes, writeBytes, writeBytes
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sendSleep

public int sendSleep
This makes a sleep during the send of a file. Important: when using softick, you must set this to 500(ms) or more, or softick will starve to death.

log2console

public static boolean log2console
Set this to true to send the log to both the console and the combo.
Since:
SuperWaba 5.66
Constructor Detail

FTP

public FTP(java.lang.String url,
           java.lang.String user,
           java.lang.String pass)
    throws java.lang.Exception,
           FTPConnectionClosedException
Opens a socket to the given URL, using a connection timeout of 15 seconds and a read timeout of 5 seconds, logs in with the user/pass.
Parameters:
url - The url as an ip or a full address to the server. The connection port is always 21
user - The user name for login
pass - The password for login

FTP

public FTP(java.lang.String url,
           java.lang.String user,
           java.lang.String pass,
           int port)
    throws java.lang.Exception,
           FTPConnectionClosedException
Opens a socket to the given URL, using a connection timeout of 15 seconds and a read timeout of 5 seconds, logs in with the user/pass.
Parameters:
url - The url as an ip or a full address to the server.
user - The user name for login
pass - The password for login
port - The port used to open the connection.

FTP

public FTP(java.lang.String url,
           java.lang.String user,
           java.lang.String pass,
           Control loggingControl)
    throws java.lang.Exception,
           FTPConnectionClosedException
Opens a socket to the given URL, using a connection timeout of 15 seconds and a read timeout of 5 seconds, logs in with the user/pass.
Parameters:
url - The url as an ip or a full address to the server. The connection port is always 21
user - The user name for login
pass - The password for login
loggingControl - The ListBox or ComboBox where the logging will be sent to.

FTP

public FTP(java.lang.String url,
           java.lang.String user,
           java.lang.String pass,
           int openTimeout,
           int readTimeout)
    throws java.lang.Exception,
           FTPConnectionClosedException
Opens a socket to the given URL, logs in with the user/pass.
Parameters:
url - The url as an ip or a full address to the server. The connection port is always 21
user - The user name for login
pass - The password for login
openTimeout - The timeout used for the socket open
readTimeout - The timeout used for read operations

FTP

public FTP(java.lang.String url,
           java.lang.String user,
           java.lang.String pass,
           int openTimeout,
           int readTimeout,
           Control loggingControl)
    throws java.lang.Exception,
           FTPConnectionClosedException
Opens a socket to the given URL, logs in with the user/pass.
Parameters:
url - The url as an ip or a full address to the server. The connection port is always 21
user - The user name for login
pass - The password for login
openTimeout - The timeout used for the socket open
readTimeout - The timeout used for read operations
loggingControl - The ListBox or ComboBox where the logging will be sent to.

FTP

public FTP(java.lang.String url,
           java.lang.String user,
           java.lang.String pass,
           int port,
           int openTimeout,
           int readTimeout,
           Control loggingControl)
    throws java.lang.Exception,
           FTPConnectionClosedException
Opens a socket to the given URL, logs in with the user/pass, at the given port.
Parameters:
url - The url as an ip or a full address to the server.
user - The user name for login
pass - The password for login
port - The port used to open the connection.
openTimeout - The timeout used for the socket open
readTimeout - The timeout used for read operations
loggingControl - The ListBox or ComboBox where the logging will be sent to.
Method Detail

quit

public void quit()
          throws java.lang.Exception,
                 FTPConnectionClosedException
Sends a quit command to the server and close this socket.

getCurrentDir

public java.lang.String getCurrentDir()
                               throws java.lang.Exception,
                                      FTPConnectionClosedException
Returns the current directory of the server.

chgDir

public void chgDir(java.lang.String name)
            throws java.lang.Exception,
                   FTPConnectionClosedException
Changes the current directory to the given one

setType

public void setType(java.lang.String type)
             throws java.lang.Exception,
                    FTPConnectionClosedException
Changes the type to ASCII or BINARY

setPort

public void setPort(java.lang.String port)
             throws java.lang.Exception,
                    FTPConnectionClosedException
Issues the command PORT

delete

public void delete(java.lang.String name)
            throws java.lang.Exception,
                   FTPConnectionClosedException
Deletes the given file

mkDir

public void mkDir(java.lang.String name)
           throws java.lang.Exception,
                  FTPConnectionClosedException
Creates a directory at the server

rmDir

public void rmDir(java.lang.String name)
           throws java.lang.Exception,
                  FTPConnectionClosedException
Removes a directory at the server

noop

public void noop()
          throws java.lang.Exception,
                 FTPConnectionClosedException
This command helps to keep the connection open.

rename

public void rename(java.lang.String nameSrc,
                   java.lang.String nameDest)
            throws java.lang.Exception,
                   FTPConnectionClosedException
Renames a file

sendFile

public int sendFile(Stream inputStream,
                    java.lang.String name)
             throws java.lang.Exception,
                    FTPConnectionClosedException
Sends a file to the server. Returns the total bytes sent. Its up to the user to check if it was the desired amount.
Parameters:
inputStream - The Stream from where the data will be read (using readBytes method)
name - The name of the destination file in the server

receiveFile

public int receiveFile(java.lang.String name,
                       Stream outputStream)
                throws java.lang.Exception,
                       FTPConnectionClosedException
Retrieves a file from the server. Returns the total bytes read.

list

public java.lang.String[] list(java.lang.String wildCard,
                               boolean includeFolders)
                        throws java.lang.Exception,
                               FTPConnectionClosedException
List the contents of the current directory.
Parameters:
wildCard - Should be *.* for all files, or any other wildcard
includeFolders - If false, only files will be returned

setLoggingControl

public void setLoggingControl(Control c)
Sets the control where the log will be displayed. Only ComboBox and ListBox are permitted.