sap ftp 处理
【转】
本文示例如何使用SAP FTP Function将文件从应用服务器传输到另外一个FTP服务器上。
DATA: BEGIN OF ig_ftp_result OCCURS 0,
line(100),
END OF ig_ftp_result.
DATA: l_path(128) TYPE c. "文件路径,必须以/结尾
DATA: l_filename(128) TYPE c. "文件名
DATA: l_ftpcommand(100) TYPE c. "FTP命令
DATA: l_handle TYPE i. "HAND
"源路径,必须以/结尾
DATA: cons_source(128) TYPE c . " VALUE '/usr/sap/SY-SYSID/SYS/src/'.
DATA: cons_dens LIKE zftpt-zpath. "目标路径
CONSTANTS cons_key TYPE i VALUE 26101957.
*// INITIALIZATION
INITIALIZATION.
*服务器上下载文件的路径
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:
l_dstlen TYPE i, "DESTINATION LEN
l_pw(64) TYPE c. "密码
* 连接FTP服务器
l_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 = '192.168.1.10'
rfc_destination = 'SAPFTPA' "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改变本地路径错误!'.
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, '改变FTP路径出现错误!'.
STOP.
ENDIF.
ENDIF.
* Change TRANSFER MODE
CLEAR l_ftpcommand.
* l_ftpcommand = 'binary'.
l_ftpcommand = 'ascii'.
PERFORM frm_ftp_command USING l_ftpcommand pr_return.
IF pr_return = '1'.
WRITE:/ sy-datum, sy-uzeit, sy-uname, '改变FTP传输模式出现错误!'.
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, '文件传输中出现错误!'.
STOP.
ENDIF.
* 断开FTP服务器
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = l_handle.
WRITE:/ sy-datum, sy-uzeit, sy-uname, '文件传输成功!'.
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
DATA: BEGIN OF ig_ftp_result OCCURS 0,
line(100),
END OF ig_ftp_result.
DATA: l_path(128) TYPE c. "文件路径,必须以/结尾
DATA: l_filename(128) TYPE c. "文件名
DATA: l_ftpcommand(100) TYPE c. "FTP命令
DATA: l_handle TYPE i. "HAND
"源路径,必须以/结尾
DATA: cons_source(128) TYPE c . " VALUE '/usr/sap/SY-SYSID/SYS/src/'.
DATA: cons_dens LIKE zftpt-zpath. "目标路径
CONSTANTS cons_key TYPE i VALUE 26101957.
*// INITIALIZATION
INITIALIZATION.
*服务器上下载文件的路径
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:
l_dstlen TYPE i, "DESTINATION LEN
l_pw(64) TYPE c. "密码
* 连接FTP服务器
l_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 = '192.168.1.10'
rfc_destination = 'SAPFTPA' "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改变本地路径错误!'.
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, '改变FTP路径出现错误!'.
STOP.
ENDIF.
ENDIF.
* Change TRANSFER MODE
CLEAR l_ftpcommand.
* l_ftpcommand = 'binary'.
l_ftpcommand = 'ascii'.
PERFORM frm_ftp_command USING l_ftpcommand pr_return.
IF pr_return = '1'.
WRITE:/ sy-datum, sy-uzeit, sy-uname, '改变FTP传输模式出现错误!'.
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, '文件传输中出现错误!'.
STOP.
ENDIF.
* 断开FTP服务器
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = l_handle.
WRITE:/ sy-datum, sy-uzeit, sy-uname, '文件传输成功!'.
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
本人常年接收SAP运维和远程项目,ECC,S/4HANA,CRM,WDA.
远程人天可谈,终身售后,有活请联系V信:18925782767(问问题免费,欢迎交流!)
!!请关注本人技术分享公众号:SAP翔子(可扫头像二维码)
每天分享新知识,博客文章也会陆续整理并迁移至公众号,与您一起共同学习