*程式邏輯結構
*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
*-----------------------------------------------------------------------
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. "傳回文本
***************************變量聲明區域*********************************
l_wa_login type zlm_login_detail. "FTP登陸資訊配置結構
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. "密碼
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.
*" 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'
user = l_wa_login-s_username "使用者名
password = l_password "密碼
host = l_wa_login-s_hostip "IP位址
rfc_destination = l_wa_login-dest "RFC邏輯目标
handle = l_handle_s "源FTP連接配接句柄
exceptions
not_connected = 1.
raise ftp_connect_faild.
*" 3. 将源FTP伺服器傳輸編碼方式轉換為ASCII
call function 'FTP_COMMAND'
handle = l_handle_s "源FTP連接配接句柄
command = con_ascii "編碼方式
tables
data = l_tab_result "傳回文本
others = 1.
raise ftp_change_code_faild.
*" 4. 建立目标系統FTP連結
l_str_length = strlen( l_wa_login-p_password ).
source = l_wa_login-p_password "密碼
user = l_wa_login-p_username "使用者名
host = l_wa_login-p_hostip "IP位址
handle = l_handle_d "目标FTP連接配接句柄
*" 5. 将目标FTP伺服器傳輸編碼方式轉換為ASCII
handle = l_handle_d "目标FTP連接配接句柄
*" 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.
"解析檔案名
concatenate l_file_dir
i_filename into l_dest_filename.
"從FTP伺服器讀檔案至内表
refresh:l_tab_result_ftptosap.
call function 'FTP_SERVER_TO_R3'
handle = l_handle_d
fname = l_dest_filename
character_mode = 'X'
text = l_tab_result_ftptosap
tcpip_error = 1
command_error = 2
data_error = 3
others = 4.
raise file_is_not_found.
endif .
append lines of l_tab_result_ftptosap to tab_data_result.
*" 7. 關閉源FTP連結
call function 'FTP_DISCONNECT'
handle = l_handle_s. "源FTP連接配接句柄
*" 8. 關閉目标FTP連結
handle = l_handle_d. "目标FTP連接配接句柄
*" 9. 關閉RFC遠端連結
call function 'RFC_CONNECTION_CLOSE'
destination = l_dest "RFC遠端連接配接
others = 1.
raise ftp_close_faild.
endfunction.