SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF }
.L0S31 { font-style: italic; color: #808080 }
.L0S32 { color: #3399FF }
.L0S33 { color: #4DA619 }
.L0S52 { color: #0000FF }
.L0S55 { color: #800080 }
.L0S70 { color: #808080 }
*FTP_CONNECT:通过账号连接FTP
*FTP_COMMAND:执行FTP操作命令(注意,程序中所有命令必须为小写)
*lcd:指定本地文件夹路径
*cd:指定FTP文件夹路径
*put<filename>:上传文件
*get<filename>:下载文件
*FTP_SERVER_TO_R3:将FTP上的指定文本文件数据读取到内表
*FTP_DISCONNECT:关闭FTP连接
DATA:key TYPE i VALUE 2610957,
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 FTP!‘.
EXIT.
ENDIF.
MESSAGE s001(00) WITH ‘FTP connect OK!‘.
CASE sy-ucomm.
WHEN ‘ONL1‘.
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. "mask_pwd
*通过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
command
= com-cmd
TABLES
data = res
EXCEPTIONS
tcpip_error
= 1
command_error
= 2
data_error
= 3
OTHERS = 4.
*当执行失败时回执数据并退出 .
IF sy-subrc <> 0.
CASE ‘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 ‘DOWNDLOAD‘.
MESSAGE s001(00) WITH ‘FTP DOWNDLOAD 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. "ftp_execute
*将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
blob
= 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. "dl_itab