【ABAP系列】SAP ABAP 实现FTP的文件上传与下载

公众号:SAP Technical
本文作者:matinal
 

【ABAP系列】SAP ABAP 实现FTP的文件上传与下载

前言部分

大家可以关注我的公众号,公众号里的排版更好,阅读更舒适。

正文部分

在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 ,
TRFCDEST LIKE RFCDES-RFCDEST,
THANDLE TYPE I. INCLUDE:<ICON>.
*定义输入界面
SELECTION-SCREEN:BEGIN OF BLOCK FTPLOGIN WITH FRAME TITLE TEXT-.
PARAMETERS: P_USER() LOWER CASE OBLIGATORY MEMORY ID USR,
P_PWD() MODIF ID PWD LOWER CASE OBLIGATORY MEMORY ID PWD,
P_HOST() MEMORY ID HOS OBLIGATORY,
FTP_PATH() MEMORY ID FPOS OBLIGATORY.
SELECTION-SCREEN:END OF BLOCK FTPLOGIN. SELECTION-SCREEN:BEGIN OF BLOCK UPLOAD WITH FRAME TITLE TEXT-.
PARAMETERS: P_UPPATH(),
P_FILE() .
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: PUSHBUTTON () 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-.
PARAMETERS: DL_PATH(),
DL_FILE() .
SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: PUSHBUTTON () GEBU USER-COMMAND DOWNLOAD.
SELECTION-SCREEN: PUSHBUTTON () 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 <> .
MESSAGE I001() WITH 'Can''t connect to FTP!'.
EXIT.
ENDIF. MESSAGE S001() 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 = ''.
MODIFY SCREEN .
CONTINUE.
ENDIF.
ENDLOOP.
ENDFORM. " *-----------------------------------------------------------------------------*
* 通过IP、用户名、密码连接FTP服务器 *
*-----------------------------------------------------------------------------*
FORM FTPCONNECT.
DATA: THOSTS(),TUSERS(),TPWORD().
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 . * 定义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 =
OTHERS = .
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 ,
CMD() TYPE C,
END OF COM.
DATA:BEGIN OF RES OCCURS ,
LINE() 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 .
IF COM-CMD <> ''.
*执行FTP指令
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
HANDLE = THANDLE
COMCOMMAND = COM-CMD
TABLES
DATA = RES
EXCEPTIONS
TCPIP_ERROR =
COMMAND_ERROR =
DATA_ERROR =
OTHERS = . *当执行失败时回执数据并退出
IF SY-SUBRC <> .
CASE SY-UCOMM.
WHEN 'UPLOAD'.
MESSAGE E001() WITH 'FTP UPLOAD FAIL!'.
WHEN 'DOWNLOAD'.
MESSAGE E001() WITH 'FTP DOWNLOAD FAIL!'.
ENDCASE.
EXIT.
ELSE. *根据不同按钮的功能码判断程序执行逻辑及回执信息,当选择从内表下载功能时转入子程序
CASE SY-UCOMM.
WHEN 'UPLOAD'.
MESSAGE S001() WITH 'FTP UPLOAD SUCCESS!'.
WHEN 'DOWNLOAD'.
MESSAGE S001() 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 ,
LINE() 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 <> .
MESSAGE E001() WITH 'Read FTP File FAIL!'.
EXIT.
ENDIF. *将内表数据下载到本地文件
CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
FILENAME = FILEPATH
FILETYPE = 'DAT'
TABLES
DATA_TAB = BLOB
EXCEPTIONS
FILE_OPEN_ERROR =
FILE_WRITE_ERROR =
INVALID_FILESIZE =
INVALID_TABLE_WIDTH =
INVALID_TYPE = . *将数据写入本地文件失败时回执错误
IF SY-SUBRC <> .
MESSAGE E001() WITH 'FTP Download By Internal table FAIL!'.
ELSE.
MESSAGE S001() WITH 'FTP Download By Internal table SUCCESS!'.
ENDIF.
ENDFORM. ​
上一篇:常用Linux命令-文件上传和下载


下一篇:用FileZilla服务器端和客户端实现本机与虚拟机之间文件上传和下载