*&---------------------------------------------------------------------*
*& Report Z001_TT_3 *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT Z001_TT_3.
"PARAMETERS: p_file TYPE char255 DEFAULT 'C:\USERS\ADMINISTRATOR\DESKTOP\22.TXT'."这个路径为了测试我给写死了
"p_host TYPE char64 DEFAULT 'xxx.xxx.xxx.xx xx',"ftp ip
"p_folder TYPE char255 DEFAULT '/home/ftp2018/SAPFiled/'LOWER CASE,"ftp 上传的路径
"p_uname TYPE char30 DEFAULT 'ftp2018' LOWER CASE,"ftp 名称
"p_pwd TYPE char30 DEFAULT 'xxxxxx' LOWER CASE."ftp 密码
DATA: p_file TYPE char255,
p_host(100) type c value 'xxx.xxx.xxx.xx xx',"ftp ip
p_folder(1000) type c value '/home/ftp2018/SAPFiled/',"ftp 上传的路径
p_uname(100) type c value 'ftp2018',"ftp 名称
p_pwd(100) type c value 'xxxxxx'*'."ftp 密码
p_file = '22.TXT'."这个就是你要下载的文件名称
"PARAMETERS: dest LIKE rfcdes-rfcdest DEFAULT 'SAPFTP',
" compress TYPE c DEFAULT 'N'.
DATA: hdl TYPE i,
key TYPE i VALUE 26101957,
slen TYPE i,
cmd(80) TYPE c.
DATA: BEGIN OF result OCCURS 0,
line(100) TYPE c,
END OF result.
slen = strlen( p_pwd ).
"slen = strlen( p_pwd ).
* "获取加密密码 保存到P_PWD
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
source = p_pwd
sourcelen = slen
key = key
IMPORTING
destination = p_pwd.
* 连接ftp服务器
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = p_uname
password = p_pwd
host = p_host
rfc_destination = 'SAPFTP'"dest
IMPORTING
handle = hdl. "连接的句柄
if sy-subrc <> 0.
write :/ sy-datum, sy-uzeit, sy-uname, '连接ftp失败!'. "MESSAGEG
stop.
endif.
"执行FTP命令 CD 打开目标ftp的文件夹
"now open the target ftp folder
concatenate 'cd' p_folder into cmd SEPARATED BY space.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = cmd
compress = 'N'"compress
TABLES
data = result
EXCEPTIONS
command_error = 1
tcpip_error = 2.
LOOP AT result.
WRITE AT / result-line.
ENDLOOP.
REFRESH result.
* 打开本地ftp文件夹
"now open the local ftp folder
concatenate 'lcd' 'C:\TEMP\' into cmd SEPARATED BY space."注意红色是你要下载到哪的路径!!
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = cmd
compress = 'N'"compress
TABLES
data = result
EXCEPTIONS
command_error = 1
tcpip_error = 2.
LOOP AT result.
WRITE AT / result-line.
ENDLOOP.
REFRESH result.
* 将本地文件放到目标ftp文件夹中
*CONCATENATE 'put ' p_file INTO cmd SEPARATED BY space.
*CALL FUNCTION 'FTP_COMMAND'
* EXPORTING
* handle = hdl
* command = cmd
* compress = 'N'"compress
* TABLES
* data = result
* EXCEPTIONS
* command_error = 1
* tcpip_error = 2.
*LOOP AT result.
* WRITE AT / result-line.
*ENDLOOP.
*REFRESH result.
"将ftp文件夹目标文件中放入本地文件
CONCATENATE 'get ' p_file INTO cmd SEPARATED BY space."如果是上传就put
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = cmd
compress = 'N'"compress
TABLES
data = result
EXCEPTIONS
command_error = 1
tcpip_error = 2.
LOOP AT result.
WRITE AT / result-line.
ENDLOOP.
REFRESH result.
* 断开FTP连接
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = hdl.
* 断开RFC链接
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
destination = 'SAPFTP'"dest
EXCEPTIONS
OTHERS = 1.