应用服务器与FRP传输
REPORT ZFTP_ERIC1114.
*本文示例如何使用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 VALUE 'eric.txt'. "文件名
DATA: L_FTPCOMMAND(100) TYPE C. "FTP命令
DATA: L_HANDLE TYPE I. "HAND
DATA: PR_RETURN TYPE C.
"源路径,必须以/结尾
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.
DATA: HDL TYPE I,
SLEN TYPE I.
*// INITIALIZATION
INITIALIZATION.
*服务器上下载文件的路径
CONCATENATE 'd:/usr/sap/' SY-SYSID '/SYS/global/' INTO CONS_SOURCE.
*// START OF SELECTION
START-OF-SELECTION.
PERFORM FRM_FTP_FILE.
*&---------------------------------------------------------------------*
*& Form frm_ftp_file
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM FRM_FTP_FILE .
DATA:
L_DSTLEN TYPE I, "DESTINATION LEN
L_PW(64) TYPE C. "密码
* 连接FTP服务器
L_PW = '123456'.
SET EXTENDED CHECK OFF.
SLEN = STRLEN( L_PW ).
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
SOURCE = L_PW
SOURCELEN = SLEN
KEY = CONS_KEY
IMPORTING
DESTINATION = L_PW.
*
**-- 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 = 'tools' "USER
PASSWORD = L_PW "PASS WORD
HOST = '118.242.16.254'
RFC_DESTINATION = 'SAPFTP' "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.
L_FTPCOMMAND = 'cd eric'.
PERFORM FRM_FTP_COMMAND USING L_FTPCOMMAND PR_RETURN.
IF PR_RETURN = '1'.
WRITE:/ SY-DATUM, SY-UZEIT, SY-UNAME, '文件传输中出现错误!'.
STOP.
ENDIF.
CLEAR L_FTPCOMMAND.
CONCATENATE 'get' 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