ponh 发表于 2015-9-20 14:43:42

sap abap 程序 中使用 FTP . <转载>

  原博 http://blog.iyunv.com/sunfeng8848/article/details/3500305
  reportz04ftp2.
data: begin of ig_ftp_result occurs 0,
      line(100),
      end of ig_ftp_result.
data: l_path(128)       type c."文件路径,必须以/结尾
data: l_filename(128)   type c value 'CH210276140222_likp.txt'."文件名
data: l_ftpcommand(100) type c."FTP命令
data: l_handle          type i.               "HAND
"源路径,必须以/结尾
data: cons_source(128) type cvalue '/usr/sap/AA2/D01/data/'.
data:cons_dens(128) type c .                      "目标路径
constants cons_key type ivalue 26101957.
datapr_return(1) type c value '0'.
*// INITIALIZATION
initialization.
*服务器上下载文件的路径
"CONCATENATE '/usr/sap/AA2/D01/data/' INTO cons_source.
*// START OF SELECTION
start-of-selection.
perform frm_ftp_file.

form frm_ftp_file .
data:
    l_dstlen          type i,               "DESTINATION LEN
    l_pw(64)          type c.               "密码长度一定要够否则出错
* 连接FTP服务器
l_pw = 'sap888'.
*-- FTP_CONNECT requires an encrypted password to work
*   CREATE THE NEW PW BASE ON LOGIN FTP PASS WORD.
call function 'HTTP_SCRAMBLE'
exporting
    source      = l_pw
    sourcelen   = 6
    key         = cons_key
importing
    destination = l_pw.
do 3 times." 连接三次,以为一次可能会不成功
*   OPEN THE FTP SERVER.
    call function 'FTP_CONNECT'
      exporting
      user            = 'sap01'   "USER
      password      = l_pw             "PASS WORD
      host            = '9.186.155.115 9980'
      rfc_destination = 'SAPFTPA'      "DEFAULT
      importing
      handle          = l_handle
      exceptions
      not_connected   = 1
      others          = 2.
    if sy-subrc = 0.
      exit.
    endif.
enddo.
if sy-subrc <> 0.
    write :/ sy-datum, sy-uzeit, sy-uname,'CONNECT FTP FAILED!'.            "MESSAGEG
    stop.
endif.

* Change local directory
clear l_ftpcommand.
concatenate 'lcd' cons_source into l_ftpcommand separated by space.
perform frm_ftp_command using l_ftpcommand pr_return.
   if pr_return = '1'.
   write:/ sy-datum, sy-uzeit, sy-uname,'FTP改变本地路径错误!'.
   stop.
   endif.
* Change ftp directory
if cons_dens <> ''.
    clear l_ftpcommand.
    concatenate 'cd' cons_dens into l_ftpcommand separated by space.
    perform frm_ftp_command using l_ftpcommand pr_return.
endif.
* Change TRANSFER MODE
clear l_ftpcommand.
*l_ftpcommand = 'binary'.
l_ftpcommand = 'ascii'.
perform frm_ftp_command using l_ftpcommand pr_return.
if pr_return = '1'.
    write:/ sy-datum, sy-uzeit, sy-uname,'改变FTP传输模式出现错误!'.
    stop.
endif.
* Put File into FTP SERVER
clear l_ftpcommand.
concatenate 'put'l_filename into l_ftpcommand separated by space.
perform frm_ftp_command using l_ftpcommand pr_return.
if pr_return = '1'.
    write:/ sy-datum, sy-uzeit, sy-uname,'文件传输中出现错误!'.
    stop.
endif.
*断开FTP服务器
call function 'FTP_DISCONNECT'
    exporting
      handle = l_handle.
write:/ sy-datum, sy-uzeit, sy-uname,'文件传输成功!'.
endform.                  " FRM_FTP_FILE
************************************************************************
*& FORM FRM_FTP_COMMAND                                                *
************************************************************************
*& FTP Command                                                         *
************************************************************************
form frm_ftp_command using pr_command pr_ret.
call function 'FTP_COMMAND'
    exporting
      handle                = l_handle
      command               = pr_command
*   COMPRESS            =
*   RFC_DESTINATION       =
*   VERIFY                =
*   IMPORTING
*   FILESIZE            =
*   FILEDATE            =
*   FILETIME            =
    tables
      data                  = ig_ftp_result
   exceptions
   tcpip_error         = 1
   command_error         = 2
   data_error            = 3
   others                = 4
            .
* Disconnect
if sy-subrc <> 0." 调用中出错立即断开连接
    pr_ret = '1'.
    call function 'FTP_DISCONNECT'
      exporting
      handle = l_handle.
    exit.
endif.
endform.                  "FRM_FTP_COMMAND
页: [1]
查看完整版本: sap abap 程序 中使用 FTP . <转载>