FORM f_ftp_file_customer TABLES p_text.
DATA: gv_user(30) TYPE c VALUE 'user name of ftp server',
gv_pwd(30) TYPE c VALUE 'password of ftp server',
gv_host(64) TYPE c VALUE 'ip address of FTP server',
gv_dest LIKE rfcdesrfcdest VALUE 'SAPFTPA',"Background RFC destination
lv_patha(20) TYPE C,
COMMANDS TYPE TABLE OF TEXT WITH HEADER LINE,
COMMAND_INDEX TYPE I,
RESULT TYPE TABLE OF TEXT WITH HEADER LINE.
DATA: gv_hdl TYPE i,
gv_doc TYPE c LENGTH 128,
lv_key TYPE i VALUE 26101957,
lv_slen TYPE i.
*HTTP_SCRAMBLE: used to scramble the password provided into a format, which is been recognized by SAP.
SET EXTENDED CHECK OFF.
lv_slen = STRLEN( gv_pwd ).
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
SOURCE = gv_pwd
sourcelen = lv_slen
key = lv_key
IMPORTING
destination = gv_pwd.
* To Connect to the Server using FTP
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = gv_user
password = gv_pwd
host = gv_host
rfc_destination = gv_dest
IMPORTING
handle = gv_hdl
EXCEPTIONS
OTHERS = 1.
"ftp Command
lv_patha = 'INVOICE'.
CONCATENATE 'cd' lv_patha INTO COMMANDS SEPARATED BY SPACE.
APPEND COMMANDS.
COMMANDS = 'ls'.
APPEND COMMANDS.
CALL FUNCTION 'FTP_COMMAND_LIST'
EXPORTING
HANDLE = gv_hdl
IMPORTING
COMMAND_INDEX = COMMAND_INDEX
TABLES
DATA = RESULT
COMMANDS = COMMANDS
EXCEPTIONS
COMMAND_ERROR = 1
TCPIP_ERROR = 2
ERROR_MESSAGE = 3
OTHERS = 4.
*FTP_R3_TO_SERVER:used to transfer the internal table data as a file to other system in the character mode.
CONCATENATE SY-DATUM+6(2)
SY-DATUM+4(2)
SY-DATUM(4)
'DOSYA ADI'
'.TXT'
INTO gv_doc.
CALL FUNCTION 'FTP_R3_TO_SERVER'
EXPORTING
handle = gv_hdl
fname = gv_doc "file path of destination system
character_mode = 'X'
TABLES
text = p_text
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
RAISING invalid_output_file.
ENDIF.
*FTP_DISCONNECT: This is used to disconnect the connection between SAP and other system.
* To disconnect the FTP
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = gv_hdl.
*RFC_CONNECTION_CLOSE:This is used to disconnect the RFC connection between SAP and other system.
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
destination = gv_dest
EXCEPTIONS
OTHERS = 1.
ENDFORM. " FTP_FILE_CUSTOMER
Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP S/4 HANA Logistics Team Lead
Contact : serkurumsal@yandex.com
Social Plugin