1. 程式人生 > >SAP ABAP 與FTP檔案互動-進站 檔案上傳到FTP伺服器

SAP ABAP 與FTP檔案互動-進站 檔案上傳到FTP伺服器

report  z04ftp2.
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 c  value '/usr/sap/AA2/D01/data/'.
data:  cons_dens(128) type c .                      "目標路徑

constants cons_key type i  value 26101957.
data  pr_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