Accomplish a FTP communication (File Transfer Protocol).
 

1 ftp

<ftp
    debug='debug'
    host='host'
    port='port'
    user='user'
    password='password'
    passive='passive'
    bufferSize='bufferSize'
    connectTimeout='connectTimeout'
    defaultTimeout='defaultTimeout'
    dataTimeout='dataTimeout'
    soTimeout='soTimeout'
    keepAliveTimeout='keepAliveTimeout'
    controlKeepAliveReplyTimeout='controlKeepAliveReplyTimeout'
    config-setSystemType='config-setSystemType'
    config-setServerLanguageCode='config-setServerLanguageCode'
    config-setDefaultDateFormat='config-setDefaultDateFormat'
    config-setRecentDateFormatSrt='config-setRecentDateFormatSrt'
    config-setServerTimeZoneId='config-setServerTimeZoneId'
/>

Exceptions

attribute 'host' required

The IP address of the FTP server has not been specified.

attribute 'user' required

An username has not been indicated.

attribute 'password' required

A password has not been specified.

ftp already in use, close it first

It is not possible to establish a FTP communication with the server because there is one in progress (for the same user and host).

no active ftp connection

It is not possible to get the FTP communication with the server because there is not established.

java.net.SocketException: Connection reset

It is not possible to establish a connection with the indicated host.

Remarks

This file transfers are performed between the application server on which the script is executed and another remote FTP server. The function <ftp> offers the functionality of a client FTP application. The different methods returns codes as specified by the FTP protocol. See details in: https://en.wikipedia.org/wiki/List_of_FTP_server_return_codes

Ver tambien

Class FtpClient: swig.stanford.edu/pub/java/javadoc/sun/net/ftp/FtpClient.html

RFC 959 - File Transfer Protocol: www.ietf.org/rfc/rfc959.txt

Example

List the content of the home directory, upload an image and list again the names of the files contained in the directory.

Copy
<xsql-script name='ftp_sample1'>
    <body>
        <ftp host='192.168.10.1' user='ftpuser' password='ftpdeister'>
            <println><ftp.list /></println>
            <ftp.put name='imagen_subida.jpg'>
                <file name="/tmp/nevada.jpg" type="absolute" />
            </ftp.put>
            <println><ftp.nlst /></println>
        </ftp>
    </body>
</xsql-script>

In the communication of the example, it is ordered to list the contents of the default folder that is accessed when logging in, using the function <ftp.list>. The list is visualized by screen with the print label <println>, appearing the following information:

Copy
drwxrwxrwx   2 ftpuser  ftpuser      4096 Feb 20 04:00 clients
-rw-r--r--   1 ftpuser  ftpuser   3321363 Feb  8 09:43 jt400.jar
-rwxr--r--   1 ftpuser  ftpuser        19 Feb 25 11:43 null_test.xml
drwxr-xr-x   3 ftpuser  ftpuser      4096 Mar 17  2004 pub
-rw-r--r--   1 ftpuser  ftpuser    102400 Dec 31 11:03 solaris.tar
-rwxr--r--   1 ftpuser  ftpuser 111083130 Jan 29 17:39 t5.zip
-rw-r--r--   1 ftpuser  ftpuser       386 Feb 25 09:24 text
drwxr-xr-x   2 ftpuser  ftpuser      4096 Feb  4 10:32 tmp

The second operation performed in the example is an upload of the snowfall.jpg file with the function <ftp.put>. This file is renamed to upload_image.jpg using the attribute name:

Copy
<ftp.put name='upload_image.jpg' /><file.open name='p:\snowfall.jpg' /></ftp.put>

Finally, a list is obtained again but in this time only with the file names of the remote folder <ftp.nlst>. The result would be:

Copy
pub
solaris.tar
t5.zip
tmp
text
clients
jt400.jar
null_test.xml
upload_image.jpg

Where upload_image is the file that was just transferred (from Application Server to the remote FTP server).

Example

List the files of the FTP server that accomplish a condition and show an alert if any of them is a folder.

Copy
<xsql-script name='ftp_sample1'>
    <body>
        <ftp host='192.168.10.1' user='ftpuser' password='ftppasswd'>
            <set name='m_list'><ftp.listFiles pattern='*2012*' /></set>
            <iterator name='m_fileobj' index='i'>
                  <in><m_list/></in>
                  <do>
                  	<println><ftp.file.getName><m_fileobj/><ftp.file.getName/>
                  	<if>
                  	    <expr><ftp.file.isDirectory><m_fileobj/></ftp.file.isDirectory></expr>
                  	    <then>
                  	        <println>Es Directorio</println>
                  	    </then>
                  	</if>
                  </do>
            </iterator>
        </ftp>
    </body>
</xsql-script>
Example

Download a file of the FTP server, storing it in a database.

Copy
<xsql-script name='ftp_sample2'>

   <body>
       <ftp host='192.168.10.1' user='ftpuser' password='ftpdeister'>
           <insert table='files' columns='image'>
               <value><ftp.get name='snowfall.jpg' /></value>
           </insert>
       </ftp>
   </body>


</xsql-script>

To download file you should use the function <ftp.get>. An application of this function will consist in being able to download files of a remote server to storew them in a database. Starting out from this premise, the XSQL-Script is being executed in an application server, a download of the image snowfall.jpg from FTP server with the address 192.168.10.1 is made and it is inserted in the image column of the table files of the database indicated in the command ws-dbscript. The column must be defined with the data type BLOB (Binary Large OBject).

Example

Print by screen the file of the working directory.

Copy
<xsql-script name='ftp_nlst_sample1'>

   <body>
       <ftp host='192.168.10.1' user='ftpuser' password='ftpdeister'>
           <println><ftp.nlst /></println>
       </ftp>
   </body>


</xsql-script>

The name of the files contained in the home directory, shown by console, would be the following:

Copy
tmp
cameras
clients
Example

Download a file.

Copy
<xsql-script name='ftp_get_sample1'>

   <body>
       <ftp host='192.168.10.1' user='ftpuser' password='ftpdeister'>
           <ftp.get name='snowfall.jpg' />
       </ftp>
   </body>


</xsql-script>
Example

Change the name of the file.

Copy
<xsql-script name='ftp_rename_sample1'>

   <body>
       <ftp host='192.168.10.1' user='ftpuser' password='ftpdeister'>
           <println><ftp.nlst /></println>
           <println><ftp.rename src='camaras' dst='videos' /></println>
           <println><ftp.nlst /></println>
       </ftp>
   </body>


</xsql-script>

The file called 'cameras' is renamed to 'videos'.

Example

Check that exist the indicated file in the home directory.

Copy
<xsql-script name='ftp_exists_sample1'>

   <body>
       <ftp host='192.168.10.1' user='ftpuser' password='ftpdeister'>
           <println><ftp.exists name='clients' /></println>
       </ftp>
   </body>


</xsql-script>

The name of the files contained in the home directory, shown by console, would be the following:

Copy
tmp
camaras
clientes
Example

Print by screen the content of the working directory.

Copy
<xsql-script name='ftp_list_sample1'>

   <body>
       <ftp host='192.168.10.1' user='ftpuser' password='ftpdeister'>
           <println><ftp.list /></println>
       </ftp>
   </body>


</xsql-script>

The content of the folder home directory visualized by console, would be the following:

Copy
drwxr-xr-x   2 ftpuser  ftpuser    225280 Sep  6 06:37 cameras
drwxr-xr-x   3 ftpuser  ftpuser    4096   Aug 30 11:06 clients
drwx------   2 (?)      vsalvador  4096   Jun 27 16:50 tmp
Example
Copy
<xsql-script name='ftp_cdup_sample1'>

   <body>
       <ftp host='192.168.10.1' user='ftpuser' password='ftpdeister'>
           <println><ftp.list /></println>
           <ftp.cwd dir='clients' />
           <println><ftp.list /></println>
           <ftp.cdup />
           <println><ftp.list /></println>
       </ftp>
   </body>


</xsql-script>

Completing the example of the tag <cwd>, you change the work directory 'clients' to the parent directory, which is '/', going up one level in the hierarchy.

Example

List the content of the home directory, access to a subdirectory and list the content of this directory.

Copy
<xsql-script name='ftp_cwd_sample1'>

   <body>
       <ftp host='192.168.10.1' user='ftpuser' password='ftpdeister'>
           <println><ftp.list /></println>
           <ftp.cwd dir='clients' />
           <println><ftp.list /></println>
       </ftp>
   </body>


</xsql-script>

The execution of the script shows the right functioning of the directory change tag. First, it visualizes by the console the content of the home directory, access to the directory specified in the attribute dir ('clients') and finally, lists again the content of the new work directory.

Example

Print by screen the name of the work directory.

Copy
<xsql-script name='ftp_pwd_sample1'>

   <body>
       <ftp host='192.168.10.1' user='ftpuser' password='ftpdeister'>
           <println><ftp.pwd /></println>
       </ftp>
   </body>


</xsql-script>

The name of the directory visualized in the console would be:

Copy
/
Example

Print by screen the name of the operative system of the remote host.

Copy
<xsql-script name='ftp_system_sample1'>

   <body>
       <ftp host='192.168.10.1' user='ftpuser' password='ftpdeister'>
           <println><ftp.system /></println>
       </ftp>
   </body>


</xsql-script>

The result by screen would be:

Copy
UNIX Type: L8
Example

Shows by screen a resultset with the content of a remote directory.

Copy
<xsql-script name='listFilesAsResultSet_sample1'>

  <body>
      <ftp host='remotehost' user='userftp' password='passftp' config-setServerTimeZoneId='CEST'>            
           <ftp.cwd dir='REP/DEL03' />
           <set name='m_dirpattern'><string>2013*</string></set>
           <!-- Without raw -->
           <println>
              <ftp.listFilesAsResultSet pattern='#m_dirpattern'/>
           </println>     


           <!-- With raw -->                     
           <println>
              <ftp.listFilesAsResultSet pattern='#m_dirpattern' raw='true'/>
           </println>                        
      </ftp>
  </body>


</xsql-script>

The result by screen would be:

Copy
+--------------+------+------+----+----+---------------------+----+
|name          |group |user  |type|size|date                 |raw |
|char          |char  |char  |char|long|datetime             |char|
+--------------+------+------+----+----+---------------------+----+
|20130108223447|user  |user  |D   |4096|02-05-2013 10:06:00  |    |
|20130109224001|user  |user  |D   |4096|02-05-2013 10:06:00  |    |
+--------------+------+------+----+----+---------------------+----+
                           
+--------------+------+------+----+----+---------------------+---------------------------------------------------------------------+
|name          |group |user  |type|size|date                 |raw                                                                  |
|char          |char  |char  |char|long|datetime             |char                                                                 |
+--------------+------+------+----+----+---------------------+---------------------------------------------------------------------+
|20130108223447|user  |user  |D   |4096|02-05-2013 10:06:00  |drwxrwxr-x   2 user     user         4096 May  2 08:06 20130108223447|
|20130109224001|user  |user  |D   |4096|02-05-2013 10:06:00  |drwxrwxr-x   2 user     user         4096 May  2 08:06 20130109224001|
+--------------+------+------+----+----+---------------------+---------------------------------------------------------------------+