正文部分
在SAP的实际应用中,经常会需要将生成的文件上传到FTP,或从FTP下载相关文档数据。本例实现将本地指定文件上传到FTP,及从FTP下载文件到本地的功能。
从FTP复制文件共有两种方式,一种是直接复制文件,另一种方式是先从FTP读取文件数据到内表,拷贝再写入指定位置文件,不过该功能仅支持文本格式文件(*.TXT,*.DAT),一般不予推荐。
相关函数:
1)FTP_CONNECT:通过账号连接FTP。
2)FTP_COMMAND:执行FTP操作命令(注意,程序中所有命令必须为小写)。
1cd:指定本地文件夹路径;
cd:指定FTP文件夹路径;
put <filename>:上传文件;
get <filename>:下载文件;
3)FTP_SERVER_TO_R3:将FTP上的指定文本文件数据读取到内表。
4)FTP_DISCONNECT:关闭FTP连接。
REPORT ZR_EXAMPLE_07 . DATA: KEY TYPE I VALUE 26101957, TRFCDEST LIKE RFCDES-RFCDEST, THANDLE TYPE I. INCLUDE:<ICON>. *定义输入界面 SELECTION-SCREEN:BEGIN OF BLOCK FTPLOGIN WITH FRAME TITLE TEXT-001. PARAMETERS: P_USER(45) LOWER CASE OBLIGATORY MEMORY ID USR, P_PWD(45) MODIF ID PWD LOWER CASE OBLIGATORY MEMORY ID PWD, P_HOST(15) MEMORY ID HOS OBLIGATORY, FTP_PATH(30) MEMORY ID FPOS OBLIGATORY. SELECTION-SCREEN:END OF BLOCK FTPLOGIN. SELECTION-SCREEN:BEGIN OF BLOCK UPLOAD WITH FRAME TITLE TEXT-002. PARAMETERS: P_UPPATH(45), P_FILE(20) . SELECTION-SCREEN: BEGIN OF LINE. SELECTION-SCREEN: PUSHBUTTON 1(20) PUBU USER-COMMAND UPLOAD. SELECTION-SCREEN: END OF LINE. SELECTION-SCREEN:END OF BLOCK UPLOAD. SELECTION-SCREEN:BEGIN OF BLOCK DOWNLOAD WITH FRAME TITLE TEXT-003. PARAMETERS: DL_PATH(45), DL_FILE(20) . SELECTION-SCREEN: BEGIN OF LINE. SELECTION-SCREEN: PUSHBUTTON 1(20) GEBU USER-COMMAND DOWNLOAD. SELECTION-SCREEN: PUSHBUTTON 24(30) SHBU USER-COMMAND ITAB_DL. SELECTION-SCREEN: END OF LINE. SELECTION-SCREEN:END OF BLOCK DOWNLOAD. AT SELECTION-SCREEN OUTPUT. * 对密码输入栏进行加密处理 PERFORM MASK_PWD. * 给各按钮加入文本及图标 WRITE ICON_OUTGOING_OBJECT AS ICON TO PUBU. CONCATENATE PUBU 'Upload FTP' INTO PUBU SEPARATED BY SPACE. WRITE ICON_INCOMING_OBJECT AS ICON TO GEBU. CONCATENATE GEBU 'FTP download' INTO GEBU SEPARATED BY SPACE. WRITE ICON_WRITE_FILE AS ICON TO SHBU. CONCATENATE SHBU 'Internal table download' INTO SHBU SEPARATED BY SPACE. AT SELECTION-SCREEN. PERFORM FTPCONNECT. IF SY-SUBRC <> 0. MESSAGE I001(00) WITH 'Can''t connect to FTP!'. EXIT. ENDIF. MESSAGE S001(00) WITH 'FTP connect OK!'. CASE SY-UCOMM. WHEN 'ONLI'. PERFORM FTPDISCONNECT. EXIT. ENDCASE.
*执行FTP功能 PERFORM FTP_EXECUTE. *关闭FTP连接 PERFORM FTPDISCONNECT. *-----------------------------------------------------------------------------* * 改变密码输入框显示属性,实现密码保护 * *-----------------------------------------------------------------------------* FORM MASK_PWD. LOOP AT SCREEN . IF SCREEN-NAME = 'P_PWD'. SCREEN-INVISIBLE = '1'. MODIFY SCREEN . CONTINUE. ENDIF. ENDLOOP. ENDFORM. " *-----------------------------------------------------------------------------* * 通过IP、用户名、密码连接FTP服务器 * *-----------------------------------------------------------------------------* FORM FTPCONNECT. DATA: THOSTS(45),TUSERS(45),TPWORD(45). THOSTS = P_HOST. TUSERS = P_USER. TPWORD = P_PWD. * 对密码数值进行加密解析处理 CALL 'AB_RFC_X_SCRAMBLE_STRING' " System Function ID 'SOURCE' FIELD TPWORD ID 'KEY' FIELD KEY ID 'SCR' FIELD 'X' ID 'DESTINATION' FIELD TPWORD ID 'DSTLEN' FIELD 64. * 定义RFC连接目标,前后台执行时不同 IF SY-BATCH = 'X'. TRFCDEST = 'SAPFTPA'. ELSE. TRFCDEST = 'SAPFTP'. ENDIF. *该函数可以定义有网关时账户密码,一般公司内部访问时无此设置 CALL FUNCTION 'FTP_CONNECT' EXPORTING USER = TUSERS PASSWORD = TPWORD HOST = THOSTS RFC_DESTINATION = TRFCDEST IMPORTING HANDLE = THANDLE EXCEPTIONS NOT_CONNECTED = 1 OTHERS = 2. ENDFORM. " FTPCONNECT *&--------------------------------------------------------------------- *& 设置结束时关闭SAP *&--------------------------------------------------------------------- FORM FTPDISCONNECT. CALL FUNCTION 'FTP_DISCONNECT' EXPORTING HANDLE = THANDLE. ENDFORM. " FTPDISCONNECT *&---------------------------------------------------------------------* *& 通过SAP执行命令上传或下载FTP文件 *&---------------------------------------------------------------------* FORM FTP_EXECUTE. DATA:TSUBRC LIKE SY-SUBRC. DATA:BEGIN OF COM OCCURS 0, CMD(100) TYPE C, END OF COM. DATA:BEGIN OF RES OCCURS 0, LINE(100) TYPE C, END OF RES. *指定FTP文件夹路径 IF NOT FTP_PATH IS INITIAL. CONCATENATE 'cd' FTP_PATH INTO COM-CMD SEPARATED BY ' '. APPEND COM. ENDIF. CASE SY-UCOMM. WHEN 'UPLOAD'. *指定上传文件夹路径及上传文件命令 CONCATENATE 'lcd' P_UPPATH INTO COM-CMD SEPARATED BY ' '. APPEND COM. CONCATENATE 'put' P_FILE INTO COM-CMD SEPARATED BY ' '. APPEND COM. WHEN 'DOWNLOAD'. *指定下载文件夹路径及下载文件命令 CONCATENATE 'lcd' DL_PATH INTO COM-CMD SEPARATED BY ' '. APPEND COM. CONCATENATE 'get' DL_FILE INTO COM-CMD SEPARATED BY ' '. APPEND COM. ENDCASE. LOOP AT COM FROM 1. IF COM-CMD <> ''. *执行FTP指令 CALL FUNCTION 'FTP_COMMAND' EXPORTING HANDLE = THANDLE COMCOMMAND = COM-CMD TABLES DATA = RES EXCEPTIONS TCPIP_ERROR = 1 COMMAND_ERROR = 2 DATA_ERROR = 3 OTHERS = 4. *当执行失败时回执数据并退出 IF SY-SUBRC <> 0. CASE SY-UCOMM. WHEN 'UPLOAD'. MESSAGE E001(00) WITH 'FTP UPLOAD FAIL!'. WHEN 'DOWNLOAD'. MESSAGE E001(00) WITH 'FTP DOWNLOAD FAIL!'. ENDCASE. EXIT. ELSE. *根据不同按钮的功能码判断程序执行逻辑及回执信息,当选择从内表下载功能时转入子程序 CASE SY-UCOMM. WHEN 'UPLOAD'. MESSAGE S001(00) WITH 'FTP UPLOAD SUCCESS!'. WHEN 'DOWNLOAD'. MESSAGE S001(00) WITH 'FTP DOWNLOAD SUCCESS!'. WHEN 'ITAB_DL'. PERFORM DL_ITAB USING THANDLE DL_PATH DL_FILE. ENDCASE. ENDIF. ENDIF. ENDLOOP. CLEAR:COM,RES,TSUBRC. REFRESH:COM,RES. ENDFORM. " FTPPUTFILE
*---------------------------------------------------------------------* * 将FTP文本类型文件数据读取到内表 * *---------------------------------------------------------------------* FORM DL_ITAB USING THANDLE TYPE I VALUE(FILEPATH) TYPE C FILENAME TYPE C. DATA: BEGIN OF BLOB OCCURS 0, LINE(255) TYPE C, END OF BLOB. *连接字符串定义本地文件具体路径 CONCATENATE FILEPATH FILENAME INTO FILEPATH. *将FTP文本类型文件数据读取到内表 *若所读FTP文件为非文本文件(*.txt,*.dat)时将出现错误 CALL FUNCTION 'FTP_SERVER_TO_R3' EXPORTING HANDLE = THANDLE FNAME = FILENAME TABLES BLOBBLOB = BLOB. *读取文件失败时退出程序并回执错误 IF SY-SUBRC <> 0. MESSAGE E001(00) WITH 'Read FTP File FAIL!'. EXIT. ENDIF. *将内表数据下载到本地文件 CALL FUNCTION 'WS_DOWNLOAD' EXPORTING FILENAME = FILEPATH FILETYPE = 'DAT' TABLES DATA_TAB = BLOB EXCEPTIONS FILE_OPEN_ERROR = 1 FILE_WRITE_ERROR = 2 INVALID_FILESIZE = 3 INVALID_TABLE_WIDTH = 4 INVALID_TYPE = 5. *将数据写入本地文件失败时回执错误 IF SY-SUBRC <> 0. MESSAGE E001(00) WITH 'FTP Download By Internal table FAIL!'. ELSE. MESSAGE S001(00) WITH 'FTP Download By Internal table SUCCESS!'. ENDIF. ENDFORM.
操作步骤和代码,点击进入:SAP ABAP 实现FTP的文件上传与下载