matinal:SAP ABAP 如何读取FTP文件

2023-11-07 08:11:02 浏览数 (2)

SAP账号添加权限对象S_ADMI_FCD

在表SAPFTP_SERVERS维护ftp服务器地址,建议直接输入*

代码语言:javascript复制
TYPES: BEGIN OF ty_ftp,
         line(255) TYPE c,
       END OF ty_ftp,
       BEGIN OF ty_blob,
         line(255) TYPE x,
       END OF ty_blob.

DATA: lt_ftp  TYPE TABLE OF ty_ftp,
      ls_ftp  TYPE ty_ftp,
      lt_blob TYPE TABLE OF ty_blob.

DATA: lv_server(255) TYPE c VALUE '192.168.xx.xx', "服务器地址
      lv_user(255)   TYPE c VALUE 'user',          "账号
      lv_pwd(255)    TYPE c VALUE 'xxxx',          "密码
      lv_dest        TYPE rfcdest VALUE 'SAPFTP',  "操作标识
      lv_handle      TYPE i,
      lv_len         TYPE i,
      lv_key         TYPE i VALUE 26101957,
      lv_cmd(255)    TYPE c,
      lv_dir(255)    TYPE c VALUE 'IQC/10000568057', "指定目录
      lv_blob_len    TYPE i,
      lv_xstr        TYPE xstring.

DATA: lv_msg TYPE string.

lv_len = strlen( lv_pwd ).

"密码加密
CALL FUNCTION 'HTTP_SCRAMBLE'
  EXPORTING
    source      = lv_pwd "原始密码
    sourcelen   = lv_len "密码长度
    key         = lv_key "加密方式
  IMPORTING
    destination = lv_pwd. "加密密码

"连接ftp服务器
CALL FUNCTION 'FTP_CONNECT'
  EXPORTING
    host            = lv_server
    user            = lv_user
    password        = lv_pwd
    rfc_destination = lv_dest
  IMPORTING
    handle          = lv_handle
  EXCEPTIONS
    OTHERS          = 1.

IF sy-subrc <> 0.
  "获取错误
  CALL FUNCTION 'MESSAGE_TEXT_BUILD'
    EXPORTING
      msgid               = sy-msgid
      msgnr               = sy-msgno
      msgv1               = sy-msgv1
      msgv2               = sy-msgv2
      msgv3               = sy-msgv3
      msgv4               = sy-msgv4
    IMPORTING
      message_text_output = lv_msg.

ELSE.

  "执行CMD命令(列出指定文件夹下所有的文件)
  CONCATENATE 'nlist' lv_dir INTO lv_cmd SEPARATED BY space.

  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle  = lv_handle
      command = lv_cmd
    TABLES
      data    = lt_ftp
    EXCEPTIONS
      OTHERS  = 2.

  LOOP AT lt_ftp INTO ls_ftp WHERE line CS lv_dir.

    "读取文件
    CALL FUNCTION 'FTP_SERVER_TO_R3'
      EXPORTING
        handle        = lv_handle
        fname         = ls_ftp-line
      IMPORTING
        blob_length   = lv_blob_len
      TABLES
        blob          = lt_blob
      EXCEPTIONS
        tcpip_error   = 1
        command_error = 2
        data_error    = 3
        OTHERS        = 4.

    "二进制转XString
    CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
      EXPORTING
        input_length = lv_blob_len
      IMPORTING
        buffer       = lv_xstr
      TABLES
        binary_tab   = lt_blob
      EXCEPTIONS
        failed       = 1
        OTHERS       = 2.

    "将xstring内容调用外部系统文件上传http接口

  ENDLOOP.

  "关闭连接
  CALL FUNCTION 'FTP_DISCONNECT'
    EXPORTING
      handle = lv_handle
    EXCEPTIONS
      OTHERS = 4.

ENDIF.

cl_demo_output=>write( lv_msg ).
cl_demo_output=>write( lt_ftp ).
cl_demo_output=>display(  ).

SAP连接FTP标准示例程序

RSFTP001                       SAPFTP 版本 RSFTP002                       执行 FTP 命令 RSFTP003                       测试 RSFTP004                       FTP 复制 RSFTP005                       SAPFTP 检查 RSFTP006                       FTP 命令清单 RSFTP007                       测试 FB:FTP_SERVER_TO_R3 / FTP_R3_TO_SERVER RSFTP008                       测试 FB:FTP_CLIENT_TO_R3 / FTP_R3_TO_CLIENT RSFTP009                       Test FTP put with Verify RSFTP011                       FTP Copy

0 人点赞