以下是两个FTP服务配置表:
下面是函数参数列表:
FUNCTION zmm_data_to_ftp.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(IM_FUNID) LIKE ZMWHIIBTFTP01-FUNID
*" REFERENCE(IM_FILENAME) LIKE RLGRAP-FILENAME
*" REFERENCE(IM_RFCDEST) LIKE RFCDES-RFCDEST DEFAULT 'SAPFTPA'
*" REFERENCE(IM_FLG_ADD) TYPE XFLAG OPTIONAL
*" TABLES
*" TEXT OPTIONAL
*" EXCEPTIONS
*" NO_FUNCTION_ID
*" NOT_CONNECTED
*" FTP_PATH_ERROR
*" NOT_FTP_DATA
*"----------------------------------------------------------------------
CONSTANTS: c_key TYPE i VALUE 26101957."密钥
"SAPFTPA:表示以SAP服务器为目的地,上传与下载都会放在SAP服务器上;SAPFTP:以Client端为目的地
CONSTANTS: c_rfcdest LIKE rfcdes-rfcdest VALUE 'SAPFTPA'.
DATA: flg_noftp TYPE c.
DATA:g_pwd TYPE xuncode .
DATA:g_error,
g_str(99),
g_slen TYPE i,
g_hdl TYPE i,
g_cmd(120).
DATA:i_result TYPE TABLE OF text WITH HEADER LINE.
DATA:l_file TYPE char128.
CLEAR flg_noftp.
* temp file name 用户名 日期 时间 传入的文件名
CONCATENATE sy-uname sy-datum sy-uzeit im_filename INTO l_file.
* 从自定义表中读取(IP,user,pssword)
CLEAR: zmwhiibtftp01,zmwhiibtftp02.
SELECT SINGLE
zmwhiibtftp02~ftpip
zmwhiibtftp02~usrnm
zmwhiibtftp02~passwd
zmwhiibtftp02~filepathf
zmwhiibtftp02~filepaths
INTO
(zmwhiibtftp02-ftpip,
zmwhiibtftp02-usrnm,
zmwhiibtftp02-passwd,
zmwhiibtftp02-filepathf,"FTP服务器目录
zmwhiibtftp02-filepaths"SAP服务器或Client端目录
)
FROM zmwhiibtftp01 INNER JOIN zmwhiibtftp02
ON zmwhiibtftp02~funid = zmwhiibtftp01~funid
WHERE zmwhiibtftp01~funid = im_funid."根据 funcid 联合查询
IF sy-subrc <> 0.
RAISE no_function_id.
ENDIF.
* 将密码转化为SAP的格式
g_slen = STRLEN( zmwhiibtftp02-passwd ).
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
SOURCE = zmwhiibtftp02-passwd
sourcelen = g_slen
key = c_key
IMPORTING
destination = g_pwd."加密密码
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = zmwhiibtftp02-usrnm
password = g_pwd
host = zmwhiibtftp02-ftpip
rfc_destination = c_rfcdest
IMPORTING
handle = g_hdl
EXCEPTIONS
not_connected = 1.
IF sy-subrc <> 0.
RAISE not_connected.
ENDIF.
* 进入指定的FTP服务器目录
CLEAR g_str.
CONCATENATE 'cd' zmwhiibtftp02-filepathf INTO g_str SEPARATED BY space.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = g_hdl
command = g_str
TABLES
data = i_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.
IF sy-subrc <> 0 .
RAISE ftp_path_error.
ENDIF.
* 进入到指定的SAP服务器上的目录
IF zmwhiibtftp02-filepaths IS NOT INITIAL.
CLEAR g_str.
CONCATENATE 'lcd' zmwhiibtftp02-filepaths INTO g_str SEPARATED BY space.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = g_hdl
command = g_str
TABLES
data = i_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.
IF sy-subrc <> 0 .
RAISE ftp_path_error.
ENDIF.
ENDIF.
* get ftp file to sap server as temp file if file exist and in append mode
* 如果FTP服务器上有存在 im_filename 文件,则取到SAP服务器上并以l_file为临时文件名
IF im_flg_add = 'X'.
CLEAR g_str.
CONCATENATE 'get' im_filename l_file INTO g_str SEPARATED BY space.
CONDENSE g_str.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = g_hdl
command = g_str
TABLES
data = i_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.
ENDIF.
* internal table data to sap server by dataset
* 将内表中的数据 APPEND 到SAP服务器上的临时文件上
OPEN DATASET l_file FOR APPENDING IN LEGACY TEXT MODE CODE PAGE '8400' WITH WINDOWS LINEFEED
IGNORING CONVERSION ERRORS. "S20090224085328
IF sy-subrc = 0.
LOOP AT text.
TRANSFER text TO l_file.
ENDLOOP.
CLOSE DATASET l_file .
ENDIF.
**********
* 实际上可以使用 FTP_R3_TO_SERVER: 直接将SAP的内表数据传输到其他系统,而不用先在
* SAP服务器或Client上生成临时文件
**********
* put sap server file to ftp server by ftp command
* 将SAP服务器止的临时文件l_file通过FTP上传到FTP服务器上并以im_filename文件名存储
CLEAR g_str.
**{modi by zhengjun S20120217100617
* CONCATENATE 'put' l_file im_filename INTO g_str SEPARATED BY space.
concatenate 'put "' l_file '" "' im_filename '"' into g_str .
**}
CONDENSE g_str.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = g_hdl
command = g_str
TABLES
data = i_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.
IF sy-subrc <> 0 .
flg_noftp = 'X'.
ENDIF.
* delete sap server temp file.
* 删除SAP服务器上生成的临时文件
DELETE DATASET l_file.
IF flg_noftp = 'X'.
RAISE not_ftp_data.
ENDIF.
* quit ftp
* 退出FTP 服务
CLEAR g_str.
g_str = 'quit'.
CONDENSE g_str.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = g_hdl
command = g_str
TABLES
data = i_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.
* disconnect
* 关闭SAP与其他系统的连接
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = g_hdl.
*关闭SAP与其他系统的RFC连接.
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
destination = c_rfcdest
EXCEPTIONS
OTHERS = 1.
ENDFUNCTION.
使用zmm_data_to_ftp:
types: line(1800) .
data: txt type standard table of line with header line.
data: l_funid like zmwhiibtftp01-funid .
data: l_filename like rlgrap-filename.
l_filename = p_file.
select single funid into l_funid from zmwhiibtftp01
where funsk = p_funsk.
call function 'ZMM_DATA_TO_FTP'
exporting
im_funid = l_funid
im_filename = l_filename
im_rfcdest = 'SAPFTPA'
tables
text = txt
exceptions
no_function_id = 1
not_connected = 2
ftp_path_error = 3
not_ftp_data = 4
others = 5.
DATA: file TYPE string VALUE `jzjflights.dat`,
wa TYPE spfli.
OPEN DATASET file FOR OUTPUT IN BINARY MODE.
SELECT *
FROM spfli
INTO wa.
TRANSFER wa TO file. "写
ENDSELECT.
CLOSE DATASET file.
DATA: file TYPE string VALUE `jzjflights.dat`,
wa TYPE spfli.
OPEN DATASET file FOR INPUT IN BINARY MODE.
DO.
"由于没有使用MAXIMUM LENGTH选项,所以每次读取的最大字节数由wa所占字节数决定
READ DATASET file INTO wa. "读
IF sy-subrc = 0.
WRITE: / wa-carrid,
wa-connid,
wa-countryfr,
wa-cityfrom,
wa-cityto,
wa-fltime,
wa-distance.
ELSE.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET file.