Академический Документы
Профессиональный Документы
Культура Документы
htm
b) SAPFTPA (for running in Application server)(This is used for file transfer in background.
2) Code sample for developing FTP programs can be got from report program
a) RSFTP002
b) RSFTP007
c) RSFTP008
3) Sample code for getting customer details as tab delimited .TXT file in background.
*&---------------------------------------------------------------------*
*& Report ZCUSTOMER
*&
*&---------------------------------------------------------------------*
*&-Customer -All new/changed customer records created during the period.
*&---------------------------------------------------------------------*
*LedgerKey-------------Char(50)
*AccountName-----------Char(80)
*AccountReference -----Char(50)
*Address1 -------------Char(80)
*Address2 -------------Char(80)
*Town -----------------Char(50)
*County / State -------Char(50)
*Post Code / ZIP ------Char(20)
*Country---------------Char(2)
*AccountCurrencyCode --Char(3)
*ContactTelephone -----Char(20)
REPORT zcustomer.
TABLES: kna1,knb1.
TYPES: BEGIN OF st_customer,
ledgerkey TYPE char50,
name1 TYPE kna1-name1,
kunnr TYPE kna1-kunnr,
name2 TYPE kna1-name2,
stras TYPE kna1-stras,
ort01 TYPE kna1-ort01,
regio TYPE kna1-regio,
pstlz TYPE kna1-pstlz,
land1 TYPE kna1-land1,
waers TYPE knvv-waers,
telf1 TYPE kna1-telf1,
END OF st_customer.
TYPES : BEGIN OF st_cus,
ledgerkey(50) TYPE c, "LedgerKey
wrk_delim1 TYPE x ,
name1(80) TYPE c, "AccountName
wrk_delim2 TYPE x ,
kunnr(50) TYPE c, "AccountReference
wrk_delim3 TYPE x ,
name2(80) TYPE c, "Address1
wrk_delim4 TYPE x ,
stras(80) TYPE c, " Address2
wrk_delim5 TYPE x ,
ort01(50) TYPE c, " Town
wrk_delim6 TYPE x ,
regio(50) TYPE c, " County / State
wrk_delim7 TYPE x ,
pstlz(10) TYPE c, "Post Code / ZIP
wrk_delim8 TYPE x ,
land1(2) TYPE c, "Country
wrk_delim9 TYPE x ,
waers(3) TYPE c, "AccountCurrencyCode
wrk_delim10 TYPE x ,
telf1(20) TYPE c, "ContactTelephone
END OF st_cus .
DATA: wrk_file TYPE char200.
DATA: it_customer TYPE STANDARD TABLE OF st_customer,
wa_customer LIKE LINE OF it_customer.
DATA: it_dat TYPE STANDARD TABLE OF st_cus,
wa_dat LIKE LINE OF it_dat.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-001.
SELECT-OPTIONS:cust_no FOR kna1-kunnr,"customer no
com_code FOR knb1-bukrs,"company code
country FOR kna1-land1, "country
creat_on FOR kna1-erdat default sy-datum. "created or changed on
SELECTION-SCREEN END OF BLOCK b2.
START-OF-SELECTION.
IF creat_on-high IS INITIAL.
creat_on-high = creat_on-low.
ENDIF.
CONCATENATE sy-mandt '_1_' creat_on-low '_' creat_on-high '_Cust.txt' INTO
wrk_file.
PERFORM customer_data_select. "Customer Data File
PERFORM build_template_data.
PERFORM ftp_file_customer.
*&---------------------------------------------------------------------*
*& Form CUSTOMER_DATA_SELECT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM customer_data_select .
SELECT a~name1 a~name2 a~kunnr a~stras a~ort01 a~regio a~pstlz a~land1 a~telf1
"Details of the Customer based on KNA1.
FROM kna1 AS a
INNER JOIN knb1 AS b ON a~kunnr = b~kunnr
INTO CORRESPONDING FIELDS OF TABLE it_customer
WHERE a~kunnr IN cust_no
AND ( ( a~erdat IN creat_on OR a~updat IN creat_on ) OR
( b~erdat IN creat_on OR b~updat IN creat_on ) )
AND a~land1 IN country
AND b~bukrs IN com_code.
IF sy-subrc = 0.
LOOP AT it_customer INTO wa_customer.
SELECT SINGLE waers FROM knvv INTO wa_customer-waers
WHERE kunnr = wa_customer-kunnr..
wa_customer-ledgerkey = '12345'.
MODIFY it_customer FROM wa_customer.
ENDLOOP.
ENDIF.
ENDFORM. " CUSTOMER_DATA_SELECT
*&---------------------------------------------------------------------*
*& Form BUILD_TEMPLATE_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM build_template_data .
LOOP AT it_customer INTO wa_customer.
MOVE-CORRESPONDING wa_customer TO wa_dat.
wa_dat-wrk_delim1 = '09'. "for adding tab after each field
wa_dat-wrk_delim2 = '09'.
wa_dat-wrk_delim3 = '09'.
wa_dat-wrk_delim4 = '09'.
wa_dat-wrk_delim5 = '09'.
wa_dat-wrk_delim6 = '09'.
wa_dat-wrk_delim7 = '09'.
wa_dat-wrk_delim8 = '09'.
wa_dat-wrk_delim9 = '09'.
wa_dat-wrk_delim10 = '09'.
APPEND wa_dat TO it_dat.
ENDLOOP.
ENDFORM. " BUILD_TEMPLATE_DATA
*&---------------------------------------------------------------------*
*& Form FTP_FILE_CUSTOMER
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM ftp_file_customer .
DATA: l_user(30) TYPE c VALUE 'ZTEST', "user name of ftp server
l_pwd(30) TYPE c VALUE '1234', "password of ftp server
l_host(64) TYPE c VALUE '111.2.1.198', "ip address of FTP server
l_dest LIKE rfcdes-rfcdest VALUE 'SAPFTPA'."Background RFC destination
DATA: w_hdl TYPE i,
c_key TYPE i VALUE 26101957,
l_slen TYPE i.
*HTTP_SCRAMBLE: used to scramble the password provided in a format recognized by SAP.
SET EXTENDED CHECK OFF.
l_slen = STRLEN( l_pwd ).
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
SOURCE = l_pwd
sourcelen = l_slen
key = c_key
IMPORTING
destination = l_pwd.
* To Connect to the Server using FTP
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = l_user
password = l_pwd
host = l_host
rfc_destination = l_dest
IMPORTING
handle = w_hdl
EXCEPTIONS
OTHERS = 1. .
*FTP_R3_TO_SERVER:used to transfer the internal table data as a file to other system in the
character mode.
CALL FUNCTION 'FTP_R3_TO_SERVER'
EXPORTING
handle = w_hdl
fname = wrk_file "file path of destination system
character_mode = 'X'
TABLES
text = it_dat
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 = w_hdl.
*RFC_CONNECTION_CLOSE:This is used to disconnect the RFC connection between SAP and other
system.
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
destination = l_dest
EXCEPTIONS
OTHERS = 1.
ENDFORM. " FTP_FILE_CUSTOMER
On running this report in background a file will be created in ftp server location.
----------------------------------------------------------------
https://wiki.scn.sap.com/wiki/display/Snippets/Sample+program+for+FTP+Transfer+of+Files+via+ABAP+Program
Created by Former Member, last modified by Former Member on Jun 08, 2013
Go to start of metadata
The below program is a sample code on how to transfer files from a FTP Directory into SAP Application server directory. This program will :
(2) Copy the files in to the SAP directory - SAP Application Server
Additionally a Log table has been created which will track the status of the file.
Code as below :
Error rendering macro 'code': Invalid value specified for parameter 'lang'
REPORT zftptrasfr
*T Y P E S
************************************************************************
TYPES:
BEGIN OF x_cmdout,
line(100) TYPE c,
END OF x_cmdout.
TYPE-POOLS : slis.
************************************************************************
* D A T A
************************************************************************
w_hdl TYPE i,
w_slen TYPE i,
*Constant declarations
************************************************************************
* P A R A M E T E R S
************************************************************************
************************************************************************
************************************************************************
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
CASE screen-name.
screen-invisible = '1'.
MODIFY SCREEN.
ENDCASE.
ENDLOOP.
************************************************************************
* S T A R T - O F - S E L E C T I O N
************************************************************************
START-OF-SELECTION.
PERFORM ftp_connect.
*Find all files in the directory and store inside the Log table
PERFORM log_files.
IF it_iplftp[] IS INITIAL.
LEAVE LIST-PROCESSING.
ENDIF.
*Change the local save directory and download to SAP application server.
PERFORM move_files.
PERFORM close_ftp.
************************************************************************
*END-OF-SELECTION
************************************************************************
END-OF-SELECTION.
*Display report.
PERFORM disp_res.
*&---------------------------------------------------------------------
*&---------------------------------------------------------------------
*----------------------------------------------------------------------
FORM ftp_connect .
EXPORTING
source = p_pwd
sourcelen = w_slen
key = w_key
IMPORTING
destination = p_pwd.
EXPORTING
user = p_user
password = p_pwd
host = p_host
rfc_destination = c_dest
IMPORTING
handle = w_hdl
EXCEPTIONS
not_connected = 1
OTHERS = 2.
IF sy-subrc <> 0.
*Message will arise in case of any issues in connecting to the FTP server.
LEAVE LIST-PROCESSING.
ENDIF.
*&---------------------------------------------------------------------
*&---------------------------------------------------------------------
*----------------------------------------------------------------------
FORM log_files .
EXPORTING
handle = w_hdl
command = w_cmd
compress = 'N'
TABLES
data = it_cmdout
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
ENDIF.
REFRESH it_cmdout.
CLEAR w_cmd.
w_cmd = 'ls'.
EXPORTING
handle = w_hdl
command = w_cmd
compress = 'N'
TABLES
data = it_cmdout
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
ENDIF.
wa_iplftp-file_nm = wa_cmdout-line+39(33).
wa_iplftp-log_id = w_logid.
wa_iplftp-fdate = sy-datum."WA_CMDOUT-LINE+39(8).
wa_iplftp-ftime = sy-uzeit."WA_CMDOUT-LINE+48(6).
wa_iplftp-ernam = sy-uname.
ENDLOOP.
REFRESH it_cmdout.
ENDFORM. " LOG_FILES
*&---------------------------------------------------------------------
*&---------------------------------------------------------------------
FORM move_files .
CLEAR w_cmd.
EXPORTING
handle = w_hdl
command = w_cmd
compress = 'N'
TABLES
data = it_cmdout
EXCEPTIONS
command_error = 1
tcpip_error = 2.
IF sy-subrc <> 0.
ENDIF.
REFRESH it_cmdout.
stat EQ 'CR'.
l_indx = sy-tabix.
CLEAR w_cmd.
REFRESH it_cmdout.
EXPORTING
handle = w_hdl
command = w_cmd
compress = 'N'
TABLES
data = it_cmdout
EXCEPTIONS
command_error = 1
tcpip_error = 2.
IF sy-subrc <> 0.
ENDIF.
IF sy-subrc EQ 0.
wa_iplftp-stat = 'CR'.
wa_iplftp-mess = text-004.
ELSE.
wa_iplftp-stat = 'EF'.
wa_iplftp-mess = text-005.
ENDIF.
ENDIF.
CLEAR w_cmd.
EXPORTING
handle = w_hdl
command = w_cmd
compress = 'N'
TABLES
data = it_cmdout
EXCEPTIONS
command_error = 1
tcpip_error = 2.
IF sy-subrc <> 0.
ENDIF.
ENDLOOP.
IF sy-subrc NE 0.
ENDIF.
*&---------------------------------------------------------------------
*&---------------------------------------------------------------------
FORM close_ftp .
EXPORTING
handle = w_hdl.
EXPORTING
destination = c_dest
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
ENDIF.
*&---------------------------------------------------------------------
*&---------------------------------------------------------------------
FORM disp_res .
l_layout-colwidth_optimize = 'X'.
EXPORTING
i_callback_program = sy-repid
i_structure_name = 'ZTBL_IPL_FTP'
is_layout = l_layout
TABLES
t_outtab = it_iplftp
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
ENDIF.
1 Comment
1.
Former Member
This Program will check file name on ftp server and after Execute ur program. if ur filename is already there that time u got message 'FILE
ALLREADY EXIST.....' and file is not there then ur program will Execute.
-------------------------------------------------------------------------
REPORT zftp_backgroud_app.
pwd = 'password'.
cmd = 'ascii'.
cmd2 = 'cd /tmp'.
compress = 'N'.
----------------------------------------------------------------------------------------------------------------------
https://www.samplecodeabap.com/sftp-sap-ecc-abap/
We use FTP to transfer sales order information to Hauliers; this is important piece of information for them to plan their resources (Drivers,
Truck etc.). We are using FTP on site-to-site VPN to make communication secure. For infrastructure reasons we are moving away from
site-to-site VPN and requirement is to use SFTP.
SAP ECC does not support sftp this is how I managed to achieve sftp communication using open source Winscp sftp client. Note that we
have SAP server installed on Windows 2003 server and you will have to adapt some steps for other server OS.
Download Winscp portable executables from http://winscp.net/eng/download.php . It’s a zip file, with an exe and com file. Unzip the
content and put it on server from where your SAP can execute them. I have kept mine in externexe/sftp folder. One first execution it will
automatically create ini file so don’t worry if you haven’t got ini file.
Before we start with SAP side of development let’s check if this client is working.
Usually Winscp client opens up a session with server where you execute commands and close it when done. But here we want to open
session, execute commands and close session all in one go and fortunately Winscp does support this.
A simple dir command along with open session and exit command look like this
Option batch abort prevent session to seek for user input if anything goes wrong. For example, if your user id don’t have authorisation on
sftp server then Winscp client will prompt for Retry which will not be desirable if we are executing command from SAP. You can execute
multiple commands here just add them to list before exit in double quotes.
A file transfer command from a specific directory on client (F:/data) to specific directory on sftp server (in) will look like this.
To test the command from SAP press execute and input “open “sftp://username:password@host_ip”” “dir” “exit” in additional
parameters and execute.
You should get same output which you got in command prompt. Notice variable return code. Zero value signifies successful execution.
Step 4: Calling external command ZFTP using function module SXPG_COMMAND_EXECUTE.
This is sample code will give you an idea how to call external command we just created. You will have to make some changes to make it
work at your place.
1 METHOD execute_all_commands.
5
* Create additional parameters strings based on username, password and host ip
6
IF me->username IS INITIAL .
7
RAISE EXCEPTION TYPE zcx_sfp
8
EXPORTING
9
textid = zcx_sfp=>user_name_empty .
10 ENDIF.
11
12 IF me->password IS INITIAL .
14 EXPORTING
15 textid = zcx_sfp=>password_empty .
16 ENDIF.
17
IF me->host_ip IS INITIAL .
18
RAISE EXCEPTION TYPE zcx_sfp
19
EXPORTING
20
textid = zcx_sfp=>password_empty .
21
ENDIF.
22
27 ' "sftp://'
28 me->username
':'
29
me->password
30
'@'
31
me->host_ip
32
'""'
33 INTO lv_additional_parameters .
34
36 CONCATENATE lv_additional_parameters
37 me->additional_commmands
38 INTO lv_additional_parameters
SEPARATED BY space .
39
40
* Add exit command in the end
41
CONCATENATE lv_additional_parameters
42
'"exit"'
43
INTO lv_additional_parameters
44 SEPARATED BY space .
45
48 EXPORTING
49 textid = zcx_sfp=>command_too_long .
50 ENDIF.
51
52
56
lv_additional_param_func = lv_additional_parameters .
57
58
CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
59
EXPORTING
60 commandname = me->command_name
61 additional_parameters = lv_additional_param_func
62 IMPORTING
63 status = lv_status
64 exitcode = lv_exitcode
TABLES
65
exec_protocol = li_return
66
EXCEPTIONS
67
no_permission = 1
68
command_not_found = 2
69 parameters_too_long = 3
70 security_risk = 4
71 wrong_check_call_interface = 5
72 program_start_error = 6
73 program_termination_error = 7
x_error = 8
74
parameter_expected = 9
75
too_many_parameters = 10
76
illegal_command = 11
77
wrong_asynchronous_parameters = 12
78 cant_enq_tbtco_entry = 13
79 jobcount_generation_error = 14
80 OTHERS = 15.
81
EXPORTING
84
85 textid = zcx_sfp=>error_sftp
86 exit_code = lv_exitcode
status = lv_status
87
sftp_log = li_return .
88
ENDIF.
89
90
return = li_return .
91
92
ENDMETHOD.
93
94
95
96
97
98
99
100
--------------------------------------------------------------------------------------------------------------------