abap中,ftp上傳檔案demo
阿新 • • 發佈:2019-01-09
*&---------------------------------------------------------------------*
*& Form frm_trans_file_to_ftp
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_trans_file_to_ftp USING p_file
p_path.
DATA: user(30) TYPE c VALUE 'usname',
pwd(30) TYPE c VALUE 'password',
host(64) TYPE c VALUE 'ftpserver' ,
dest LIKE rfcdes-rfcdest VALUE 'SAPFTP',
compress TYPE c VALUE 'N'.
DATA: hdl TYPE i,
key TYPE i VALUE 26101957,
slen TYPE i.
DATA: BEGIN OF lt_commands OCCURS 0,
cmd(100) TYPE c,
END OF lt_commands.
DATA: BEGIN OF result OCCURS 0,
line (100) TYPE c,
END OF result.
SET EXTENDED CHECK OFF.
slen = strlen( pwd ).
AUTHORITY-CHECK OBJECT 'S_ADMI_FCD'
ID 'S_ADMI_FCD'
FIELD 'SFTP'.
IF sy-subrc <> 0.
MESSAGE 'no ftp authorization' TYPE 'I'.
EXIT.
ENDIF.
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
source = pwd
sourcelen = slen
key = key
IMPORTING
destination = pwd.
CONCATENATE 'lcd' p_path INTO lt_commands-cmd SEPARATED BY space.
APPEND lt_commands.
lt_commands-cmd = 'cd /inspur/packinglist/'.
APPEND lt_commands.
TRANSLATE p_file TO LOWER CASE.
CONCATENATE 'put' p_file INTO lt_commands-cmd SEPARATED BY space.
APPEND lt_commands.
*該函式可以定義有閘道器時賬戶密碼,一般公司內部訪問時無此設定
DO 5 TIMES.
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = user
password = pwd
host = host
rfc_destination = dest
IMPORTING
handle = hdl.
IF sy-subrc = 0.
EXIT.
ELSE.
WAIT UP TO 1 SECONDS.
ENDIF.
ENDDO.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
"INTO gs_out-msg
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
LOOP AT lt_commands.
IF lt_commands-cmd NE ' '.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = lt_commands-cmd
compress = compress
TABLES
data = result
EXCEPTIONS
command_error = 1
tcpip_error = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
"INTO gs_out-msg
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.
ENDIF.
ENDIF.
ENDLOOP.
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = hdl.
ENDIF.
ENDFORM. " frm_trans_file_to_ftp
*& Form frm_trans_file_to_ftp
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_trans_file_to_ftp USING p_file
p_path.
DATA: user(30) TYPE c VALUE 'usname',
pwd(30) TYPE c VALUE 'password',
host(64) TYPE c VALUE 'ftpserver'
dest LIKE rfcdes-rfcdest VALUE 'SAPFTP',
compress TYPE c VALUE 'N'.
DATA: hdl TYPE i,
key TYPE i VALUE 26101957,
slen TYPE i.
DATA: BEGIN OF lt_commands OCCURS 0,
cmd(100) TYPE c,
END OF lt_commands.
DATA: BEGIN OF result OCCURS 0,
line
END OF result.
SET EXTENDED CHECK OFF.
slen = strlen( pwd ).
AUTHORITY-CHECK OBJECT 'S_ADMI_FCD'
ID 'S_ADMI_FCD'
FIELD 'SFTP'.
IF sy-subrc <> 0.
MESSAGE 'no ftp authorization' TYPE 'I'.
EXIT.
ENDIF.
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
source = pwd
sourcelen = slen
key = key
IMPORTING
destination = pwd.
CONCATENATE 'lcd' p_path INTO lt_commands-cmd SEPARATED BY space.
APPEND lt_commands.
lt_commands-cmd = 'cd /inspur/packinglist/'.
APPEND lt_commands.
TRANSLATE p_file TO LOWER CASE.
CONCATENATE 'put' p_file INTO lt_commands-cmd SEPARATED BY space.
APPEND lt_commands.
*該函式可以定義有閘道器時賬戶密碼,一般公司內部訪問時無此設定
DO 5 TIMES.
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = user
password = pwd
host = host
rfc_destination = dest
IMPORTING
handle = hdl.
IF sy-subrc = 0.
EXIT.
ELSE.
WAIT UP TO 1 SECONDS.
ENDIF.
ENDDO.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
"INTO gs_out-msg
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
LOOP AT lt_commands.
IF lt_commands-cmd NE ' '.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = lt_commands-cmd
compress = compress
TABLES
data = result
EXCEPTIONS
command_error = 1
tcpip_error = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
"INTO gs_out-msg
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.
ENDIF.
ENDIF.
ENDLOOP.
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = hdl.
ENDIF.
ENDFORM. " frm_trans_file_to_ftp