*程序逻辑结构
*1. 查询FTP链接配置信息表获取源FTP及目标FTP登陆信息
*2. 建立源系统FTP链接
*3. 将源FTP服务器传输编码方式转换为ASCII
*4. 建立目标系统FTP链接
*5. 将目标FTP服务器传输编码方式转换为ASCII
*6. 解析FTP路径下文件,返回到内表中
*7. 关闭源FTP链接
*8. 关闭目标FTP链接
*9. 关闭RFC远程链接
配置表
用户参数
定义
查询FTP链接配置信息表获取源FTP及目标FTP登陆信息
建立源系统FTP链接
将源FTP服务器传输编码方式转换为ASCII
建立目标系统FTP链接
将目标FTP服务器传输编码方式转换为ASCII
解析FTP路径下文件,返回到内表中
关闭源 链接
3. 代码:
function zlm_frpmt_ftp_download.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(I_DEST) TYPE CHAR10 OPTIONAL
*" REFERENCE(I_DES_NAME) TYPE CHAR100
*" REFERENCE(I_FILENAME) TYPE CHAR40
*" TABLES
*" TAB_DATA_RESULT STRUCTURE ZLM_STR_FILE_LINE
*" EXCEPTIONS
*" FILE_IS_NOT_FOUND
*" FTP_CONNECT_FAILD
*" FTP_CHANGE_CODE_FAILD
*" FTP_CLOSE_FAILD
*" CAN_NOT_GET_CONNECTION_INFOR
*"----------------------------------------------------------------------
* 功能模块名称 : ZLM_FRPMT_FTP_DOWNLOAD
*-----------------------------------------------------------------------
*程序逻辑结构
*1. 查询FTP链接配置信息表获取源FTP及目标FTP登陆信息
*2. 建立源系统FTP链接
*3. 将源FTP服务器传输编码方式转换为ASCII
*4. 建立目标系统FTP链接
*5. 将目标FTP服务器传输编码方式转换为ASCII
*6. 解析FTP路径下文件,返回到内表中
*7. 关闭源FTP链接
*8. 关闭目标FTP链接
*9. 关闭RFC远程链接
*-----------------------------------------------------------------------
types: begin of text_line,
line(2048) type c,
end of text_line.
***************************常量声明区域*********************************
constants:
con_ascii(5) type c value 'ascii'.
***************************内表声明区域*********************************
data:
l_tab_result_ftptosap type table of text_line ,
l_tab_result type table of text with header line. "返回文本
***************************变量声明区域*********************************
data:
l_wa_login type zlm_login_detail. "FTP登陆信息配置结构
data:
l_handle_s type i, "源FTP连接句柄
l_handle_d type i, "目标FTP连接句柄
l_dest type zlm_dest,
l_str_length type i, "密码长度
l_key type i value 26101957, "关键字
l_password(30) type c. "密码
data:
l_dest_filename(200) type c , "目标文件
l_file_dir type string.
*"---------------------------------------------------------------------*
*" 1. 查询FTP链接配置信息表获取源FTP及目标FTP登陆信息
*"---------------------------------------------------------------------*
if i_dest is initial. "如果输入参数不为空值
l_dest = 'SAPFTPA'.
else.
l_dest = i_dest.
endif.
select single * from zlm_login_detail into l_wa_login
where dest = l_dest.
if sy-subrc <> 0.
"MESSAGE e000(zlm_common) WITH con_ftp_logintab INTO l_dummy. "查询FTP配置信息数据表ZLM_LOGIN_DETAIL没有找到指定连接
raise can_not_get_connection_infor.
endif.
*"---------------------------------------------------------------------*
*" 2. 建立源系统FTP链接
*"---------------------------------------------------------------------*
l_str_length = strlen( l_wa_login-s_password ).
call function 'HTTP_SCRAMBLE'
exporting
source = l_wa_login-s_password "密码
sourcelen = l_str_length "长度
key = l_key "关键字
importing
destination = l_password. "密码
call function 'FTP_CONNECT'
exporting
user = l_wa_login-s_username "用户名
password = l_password "密码
host = l_wa_login-s_hostip "IP地址
rfc_destination = l_wa_login-dest "RFC逻辑目标
importing
handle = l_handle_s "源FTP连接句柄
exceptions
not_connected = 1.
if sy-subrc <> 0.
raise ftp_connect_faild.
endif.
*"---------------------------------------------------------------------*
*" 3. 将源FTP服务器传输编码方式转换为ASCII
*"---------------------------------------------------------------------*
call function 'FTP_COMMAND'
exporting
handle = l_handle_s "源FTP连接句柄
command = con_ascii "编码方式
tables
data = l_tab_result "返回文本
exceptions
others = 1.
if sy-subrc <> 0.
raise ftp_change_code_faild.
endif.
*"---------------------------------------------------------------------*
*" 4. 建立目标系统FTP链接
*"---------------------------------------------------------------------*
l_str_length = strlen( l_wa_login-p_password ).
call function 'HTTP_SCRAMBLE'
exporting
source = l_wa_login-p_password "密码
sourcelen = l_str_length "长度
key = l_key "关键字
importing
destination = l_password. "密码
call function 'FTP_CONNECT'
exporting
user = l_wa_login-p_username "用户名
password = l_password "密码
host = l_wa_login-p_hostip "IP地址
rfc_destination = l_wa_login-dest "RFC逻辑目标
importing
handle = l_handle_d "目标FTP连接句柄
exceptions
not_connected = 1.
if sy-subrc <> 0.
raise ftp_connect_faild.
endif.
*"---------------------------------------------------------------------*
*" 5. 将目标FTP服务器传输编码方式转换为ASCII
*"---------------------------------------------------------------------*
call function 'FTP_COMMAND'
exporting
handle = l_handle_d "目标FTP连接句柄
command = con_ascii "编码方式
tables
data = l_tab_result "返回文本
exceptions
others = 1.
if sy-subrc <> 0.
raise ftp_change_code_faild.
endif.
*"---------------------------------------------------------------------*
*" 6. 解析FTP路径下文件,返回到内表中
*"---------------------------------------------------------------------*
* 读取FTP上文件路径
select single des_addr into l_file_dir
from zftp_config
where des_name = i_des_name.
if l_file_dir is initial.
raise file_is_not_found.
endif.
"解析文件名
concatenate l_file_dir
i_filename into l_dest_filename.
"从FTP服务器读文件至内表
refresh:l_tab_result_ftptosap.
call function 'FTP_SERVER_TO_R3'
exporting
handle = l_handle_d
fname = l_dest_filename
character_mode = 'X'
tables
text = l_tab_result_ftptosap
exceptions
tcpip_error = 1
command_error = 2
data_error = 3
others = 4.
if sy-subrc <> 0.
raise file_is_not_found.
endif .
append lines of l_tab_result_ftptosap to tab_data_result.
*"---------------------------------------------------------------------*
*" 7. 关闭源FTP链接
*"---------------------------------------------------------------------*
call function 'FTP_DISCONNECT'
exporting
handle = l_handle_s. "源FTP连接句柄
*"---------------------------------------------------------------------*
*" 8. 关闭目标FTP链接
*"---------------------------------------------------------------------*
call function 'FTP_DISCONNECT'
exporting
handle = l_handle_d. "目标FTP连接句柄
*"---------------------------------------------------------------------*
*" 9. 关闭RFC远程链接
*"---------------------------------------------------------------------*
call function 'RFC_CONNECTION_CLOSE'
exporting
destination = l_dest "RFC远程连接
exceptions
others = 1.
if sy-subrc <> 0.
raise ftp_close_faild.
endif.
endfunction.