[Switch]
This document describes how to use the sap FTP function to transmit files from the application server to another FTP server.
Data: Begin of ig_ftp_result occurs 0,
Line (100 ),
End of ig_ftp_result.
Data: l_path (128) Type C. "file path, which must end with a slash (/)
Data: l_filename (128) Type C. "File Name
Data: l_ftpcommand (100) Type C. "FTP command
Data: l_handle type I. "Hand
"Source Path, which must end "/"
Data: cons_source (128) Type C. "value '/usr/SAP/SY-SYSID/sys/src /'.
Data: cons_dens like zftpt-zpath. "target path
Constants cons_key type I value 26101957.
* // Initialization
Initialization.
* Path of the downloaded file on the server
Concatenate '/usr/SAP/'sy-sysid'/sys/src/'into cons_source.
* // Start of Selection
Start-of-selection.
Perform frm_ftp_file.
Form frm_ftp_file.
Data:
Rochelle dstlen type I, "Destination Len
Rochelle Pw (64) Type C. "Password
* Connect to the FTP server
Rochelle PW = 'Password '.
* -- Ftp_connect requires an encrypted password to work
* Create the new PW base on login ftp pass word.
Call 'AB _ rfc_x_scramble_string' "pass word build Function
Id 'source' field l_pw "pass word
Id 'key' field cons_key "the key to create new pw
Id 'Scr 'field 'X'
Id 'destination' field l_pw "pass word
Id 'dstlen' field l_dstlen. "New pass word Len
Do 3 times.
* Open the FTP server.
Call function 'ftp _ connect'
Exporting
User = 'username' "User
Password = l_pw "pass word
Host = '1970. 168.1.10'
Rfc_destination = 'sapftp' "Default
Importing
Handle = l_handle
Exceptions
Not_connected = 1
Others = 2.
If sy-subrc = 0.
Exit.
Endif.
Enddo.
If sy-subrc <> 0.
Write:/sy-datum, Sy-uzeit, Sy-uname, 'connect FTP failed! '. "Messageg
Stop.
Endif.
* Change local directory
Clear l_ftpcommand.
Concatenate 'LCD' cons_source into l_ftpcommand separated by space.
Perform frm_ftp_command using l_ftpcommand pr_return.
If pr_return = '1 '.
Write:/sy-datum, Sy-uzeit, Sy-uname, 'ftp changed local path error! '.
Stop.
Endif.
* Change FTP directory
If cons_dens <> ''.
Clear l_ftpcommand.
Concatenate 'cd' cons_dens into l_ftpcommand separated by space.
Perform frm_ftp_command using l_ftpcommand pr_return.
If pr_return = '1 '.
Write:/sy-datum, Sy-uzeit, Sy-uname, 'an error occurred while changing the FTP path! '.
Stop.
Endif.
Endif.
* Change Transfer Mode
Clear l_ftpcommand.
* L_ftpcommand = 'binary '.
Rochelle ftpcommand = 'ascii '.
Perform frm_ftp_command using l_ftpcommand pr_return.
If pr_return = '1 '.
Write:/sy-datum, Sy-uzeit, Sy-uname, 'an error occurred while changing the FTP transmission mode! '.
Stop.
Endif.
* Put file into FTP Server
Clear l_ftpcommand.
Concatenate 'put' l_filename into l_ftpcommand separated by space.
Perform frm_ftp_command using l_ftpcommand pr_return.
If pr_return = '1 '.
Write:/sy-datum, Sy-uzeit, Sy-uname, 'file transfer error! '.
Stop.
Endif.
* Disconnect the FTP server
Call function 'ftp _ disconnect'
Exporting
Handle = l_handle.
Write:/sy-datum, Sy-uzeit, Sy-uname, 'file transfer successful! '.
Endform. "frm_ftp_file
**************************************** ********************************
* & Form frm_ftp_command *
**************************************** ********************************
* & FTP command *
**************************************** ********************************
Form frm_ftp_command using pr_command pr_ret.
Call function 'ftp _ command'
Exporting
Handle = l_handle
Command = pr_command
* Compress =
* Rfc_destination =
* Verify =
* Importing
* Filesize =
* Filedate =
* Filetime =
Tables
Data = ig_ftp_result
Exceptions
Tcpip_error = 1
Command_error = 2
Data_error = 3
Others = 4
.
* Disconnect
If sy-subrc <> 0.
Pr_ret = '1 '.
Call function 'ftp _ disconnect'
Exporting
Handle = l_handle.
Exit.
Endif.
Endform. "frm_ftp_command