Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Standard SAP provides loads of function module for doing FTP from an ABAP program. Some of them are:

  • HTTP_SCRAMBLE - To scramble the password
  • FTP_CONNECT - To connect to the remote system
  • FTP_COMMAND - To execute the FTP command
  • FTP_DISCONNECT - To close the FTP connection

Now instead of calling all these function modules individually, we can bundle then in a wrapper function module and call the wrapper function module.

Process Flow:

 For doing FTP the following steps needs to be performed.

1. RFC destination for FTP should be maintained in transaction SM59.

2. Get the host name or ip address of the remote server. Get the user id and password of the RFC user (these can be maintained in a custom Z table).

3. Scramble the password by calling function module 'HTTP_SCRAMBLE'. This scrambed password needs to be used for making the FTP connection.

4. Establish FTP connection by calling FM 'FTP_CONNECT'.

5. Issue FTP commands by calling function module 'FTP_COMMAND'.

6. Finally close the connection by calling function module 'FTP_DISCONNECT'

Example Code:

Name: Y_FTP

Import Parameters:

 Parameter          Description                  Type       Remarks                
 I_HOSTTarget system IDChar 40 Mandatory
 I_USERUser IDChar 30 Mandatory
 I_PASSWORDPasswordChar 30 Mandatory
I_RFC_DESTINATIONRFC DestinationRFCDEST Mandatory - default value 'SAPFTP' 
I_FILE_NAMEFile nameChar 50Mandatory
I_DIRECTIONDirection of transferChar 1Mandatory - there can be 2 values: O for outbound & I for inbound
I_LOCAL_FOLDERFull path of local directoryStringMandatory
I_REMOTE_FOLDERFull path of remote directoryStringMandatory


 

 

 

 

 

 

 

 

 

 

Export Parameters:

 Parameter          Description                  Type       Remarks                
 E_FTP_SUCCESSFTP success flagChar 1 'X' in case FTP successful
 

 

 

Changing and Tables Parameters: None

Exceptions:

Exception   Description 
FTP_CONNECTION_ERROR    Error in FTP Connection     
FTP_ERROR Error in FTP
LCD_ERROR Error in executing 'lcd'
CD_ERROR Error in executing 'cd'


 

Source Code:

FUNCTION y_ftp.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(I_HOST) TYPE CHAR40
*" REFERENCE(I_USER) TYPE CHAR30
*" REFERENCE(I_PASSWORD) TYPE CHAR30
*" REFERENCE(I_RFC_DESTINATION) TYPE RFCDEST DEFAULT 'SAPFTP'
*" REFERENCE(I_FILE_NAME) TYPE CHAR50
*" REFERENCE(I_DIRECTION) TYPE CHAR1
*" REFERENCE(I_LOCAL_FOLDER) TYPE STRING
*" REFERENCE(I_REMOTE_FOLDER) TYPE STRING
*" EXPORTING
*" REFERENCE(E_FTP_SUCCESS) TYPE CHAR1
*" EXCEPTIONS
*" FTP_CONNECTION_ERROR
*" FTP_ERROR
*" LCD_ERROR
*" CD_ERROR
*"----------------------------------------------------------------------

DATA:
l_length TYPE i,"Password length
l_password TYPE c LENGTH 30,
l_key TYPE i VALUE 26101957,
l_ftp_handle TYPE i,"FTP handle
l_cmd TYPE c LENGTH 200,"FTP command
l_error TYPE c."Error flag

 

* Connect to the FTP server
* Get the length of password
l_length = STRLEN( i_password ).

* Scramble the password
CLEAR: l_password.
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
SOURCE = i_password
sourcelen = l_length
key = l_key
IMPORTING
destination = l_password.


* Open connection to the FTP server
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = i_user
password = l_password
host = i_host
rfc_destination = i_rfc_destination
IMPORTING
handle = l_ftp_handle
EXCEPTIONS
not_connected = 1
OTHERS = 2.
IF sy-subrc = 0.

* Change the local folder using lcd command
CLEAR: l_cmd, l_error.
CONCATENATE 'lcd' i_local_folder INTO l_cmd SEPARATED BY space.
* Perform the change in local directory
* command is lcd
PERFORM execute_ftp_command USING l_ftp_handle l_cmd
CHANGING l_error.
IF l_error = 'X'.
* Close the connection
PERFORM close_ftp_connection USING l_ftp_handle.
* Raise LCD_ERROR exception
RAISE lcd_error.
ENDIF.

* Change the remote folder using cd command
CLEAR: l_cmd, l_error.
CONCATENATE 'cd' i_remote_folder INTO l_cmd SEPARATED BY space.
* Perform the change in remote directory
* command is cd
PERFORM execute_ftp_command USING l_ftp_handle l_cmd
CHANGING l_error.
IF l_error = 'X'.
* Close the connection
PERFORM close_ftp_connection USING l_ftp_handle.
* Raise exception
RAISE cd_error.
ENDIF.


* Do the actual file transfer - for outbound it will be put
* For inbound it will be get
CLEAR: l_cmd, l_error.
IF i_direction = 'O'. "outbound.
CONCATENATE 'put' i_file_name INTO l_cmd SEPARATED BY space.
ELSEIF i_direction = 'I'."inbound.
CONCATENATE 'get' i_file_name INTO l_cmd SEPARATED BY space.
ENDIF.
* Perform the actual File transfer - get or put
* command is the FTP command - get/put
PERFORM execute_ftp_command USING l_ftp_handle l_cmd
CHANGING l_error.

IF l_error = 'X'.
* Close the connection
PERFORM close_ftp_connection USING l_ftp_handle.
* Raise FTP_ERROR exception
RAISE ftp_error.

ELSE.
* Close the connection
PERFORM close_ftp_connection USING l_ftp_handle.
* FTP is successfull
e_ftp_success = 'X'.
ENDIF.


ELSE.
* Error in FTP connection
RAISE ftp_connection_error.
ENDIF.

ENDFUNCTION.

*--------------------------------------------------*
*--------------List of Subroutines -----------------*
*&---------------------------------------------------------------------*
*& Form execute_ftp_command
*&---------------------------------------------------------------------*
* This subroutine is used to issue FTP commands
*----------------------------------------------------------------------*
* -->P_FTP_HANDLE FTP Handle
* -->P_CMND FTP Command
* <--p_error Error flag
*----------------------------------------------------------------------*
FORM execute_ftp_command USING p_ftp_handle TYPE i
p_cmnd TYPE c
CHANGING p_error TYPE c.


REFRESH: it_result[].
* Issue the FTP command (p_cmnd) by calling this FM
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = p_ftp_handle
command = p_cmnd
TABLES
data = it_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.

* Error in the FTP command
IF sy-subrc <> 0.
CASE sy-subrc.
WHEN 1.
wa_result-line = 'TCP/IP communication error'(t30).
WHEN 2.
wa_result-line = 'FTP command error'(t31).
WHEN 3.
wa_result-line = 'Internal error in SAP FTP'(t32).
WHEN OTHERS.
wa_result-line = 'Unknown error in FTP command'(t33).
ENDCASE.
APPEND wa_result TO it_result.
p_error = 'X'.
ENDIF.
ENDFORM. " execute_ftp_command

*&---------------------------------------------------------------------*
*& Form close_ftp_connection
*&---------------------------------------------------------------------*
* This subroutine will close an open FTP connection
*----------------------------------------------------------------------*
* -->P_FTP_HANDLE FTP Handle
*----------------------------------------------------------------------*
FORM close_ftp_connection USING p_ftp_handle TYPE i.


* Call function FTP_DISCONNECT to close the connection
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = p_ftp_handle.

ENDFORM. " close_ftp_connection

15 Comments