sap 與 ftp 下載文件demo

*&---------------------------------------------------------------------*
*& Report  Z001_TT_3                                                   *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*
REPORT  Z001_TT_3.


"PARAMETERS: p_file   TYPE char255 DEFAULT 'C:\USERS\ADMINISTRATOR\DESKTOP\22.TXT'."這個路徑爲了測試我給寫死了
            "p_host   TYPE char64 DEFAULT 'xxx.xxx.xxx.xx xx',"ftp  ip
            "p_folder TYPE char255 DEFAULT '/home/ftp2018/SAPFiled/'LOWER CASE,"ftp 上傳的路徑
            "p_uname  TYPE char30 DEFAULT 'ftp2018' LOWER CASE,"ftp 名稱
            "p_pwd    TYPE char30 DEFAULT 'xxxxxx' LOWER CASE."ftp 密碼

DATA: p_file TYPE char255,
      p_host(100)  type c value 'xxx.xxx.xxx.xx xx',"ftp  ip
      p_folder(1000)  type c value '/home/ftp2018/SAPFiled/',"ftp 上傳的路徑
      p_uname(100)  type c value  'ftp2018',"ftp 名稱
      p_pwd(100)  type c value 'xxxxxx'*'."ftp 密碼

p_file = 'C:\USERS\ADMINISTRATOR\DESKTOP\22.TXT'."這個就是你要下載的文件名稱
"PARAMETERS: dest     LIKE rfcdes-rfcdest DEFAULT 'SAPFTP',
"            compress TYPE c DEFAULT 'N'.

DATA: hdl     TYPE i,
      key     TYPE i VALUE 26101957,
      slen    TYPE i,
      cmd(80) TYPE c.

DATA: BEGIN OF result OCCURS 0,
        line(100) TYPE c,
      END OF result.

slen = strlen( p_pwd ).
"slen = strlen( p_pwd ).

* "獲取加密密碼 保存到P_PWD
CALL FUNCTION 'HTTP_SCRAMBLE'
  EXPORTING
    source      = p_pwd
    sourcelen   = slen
    key         = key
  IMPORTING
    destination = p_pwd.

* 連接ftp服務器
CALL FUNCTION 'FTP_CONNECT'
  EXPORTING
    user            = p_uname
    password        = p_pwd
    host            = p_host
    rfc_destination = 'SAPFTP'"dest
  IMPORTING
    handle          = hdl.  "連接的句柄

   if sy-subrc <> 0.
    write :/ sy-datum, sy-uzeit, sy-uname,  '連接ftp失敗!'.            "MESSAGEG
    stop.
  endif.

 "執行FTP命令 CD 打開目標ftp的文件夾
"now open the target ftp folder
concatenate 'cd' p_folder into cmd SEPARATED BY space.

CALL FUNCTION 'FTP_COMMAND'
  EXPORTING
    handle        = hdl
    command       = cmd
    compress      = 'N'"compress
  TABLES
    data          = result
  EXCEPTIONS
    command_error = 1
    tcpip_error   = 2.
LOOP AT result.
  WRITE AT / result-line.
ENDLOOP.
REFRESH result.

* 打開本地ftp文件夾
"now open the local ftp folder
*concatenate 'lcd' 'C:\TEMP\' into cmd SEPARATED BY space.
*CALL FUNCTION 'FTP_COMMAND'
*  EXPORTING
*    handle        = hdl
*    command       = cmd
*    compress      = 'N'"compress
*  TABLES
*    data          = result
*  EXCEPTIONS
*    command_error = 1
*    tcpip_error   = 2.
*LOOP AT result.
*  WRITE AT / result-line.
*ENDLOOP.
*REFRESH result.


* 將本地文件放到目標ftp文件夾中
*CONCATENATE 'put ' p_file INTO cmd SEPARATED BY space.
*CALL FUNCTION 'FTP_COMMAND'
*  EXPORTING
*    handle        = hdl
*    command       = cmd
*    compress      = 'N'"compress
*  TABLES
*    data          = result
*  EXCEPTIONS
*    command_error = 1
*    tcpip_error   = 2.
*LOOP AT result.
*  WRITE AT / result-line.
*ENDLOOP.
*REFRESH result.

"將ftp文件夾目標文件中放入本地文件

CONCATENATE 'get ' p_file INTO cmd SEPARATED BY space."如果是上傳就put 
CALL FUNCTION 'FTP_COMMAND'
  EXPORTING
    handle        = hdl
    command       = cmd
    compress      = 'N'"compress
  TABLES
    data          = result
  EXCEPTIONS
    command_error = 1
    tcpip_error   = 2.
LOOP AT result.
  WRITE AT / result-line.
ENDLOOP.
REFRESH result.


* 斷開FTP連接
CALL FUNCTION 'FTP_DISCONNECT'
  EXPORTING
    handle = hdl.
* 斷開RFC鏈接
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
  EXPORTING
    destination = 'SAPFTP'"dest
  EXCEPTIONS
    OTHERS      = 1.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章