ABAP读写SAP服务器文件

使用方式:

1、在应用服务器中打开文件OPEN DATASET [options]此句打开文件。如果不指定任何模式选项,则文件将按二进制模式打开。如果系统不能打开文件,则将系统字段SY-SUBRC设置为8,否则SY-SUBRC返回0

2、打开文件读取OPEN DATASET FOR INPUT IN TEXT MODE ENCODING DEFAULT .(以TXT文件打开)

3、打开文件写入OPEN DATASET FOR OUTPUT IN TEXT MODE ENCODING DEFAULT .(以TXT文件打开写入,这种是打开文件完全重写)

4、打开文件追加 OPEN DATASET FOR APPENDING IN TEXT MODE ENCODING DEFAULT .(以TXT文件打开写入,这种是打开文件追加记录)

5、关闭应用服务器上的文件 CLOSE DATASET .

6、删除应用服务器上的文件 DELETE DATASET .

7、向应用服务器上文件上写入数据TRANSFER to [LENGTH ].

8、向应用服务器上文件上读取数据READ DATASET INTO [LENGTH ].

 

常用事务代码:

1.AL11,服务器上的文件

2.CG3Y 下载文件

3.CG3Z 上传文件

 

以上总结来自"蜡笔小彭"博主

 

以下是我测试的代码

写入效果:

写入后最好使用CG3Y下载上传至SAP服务器的文件看内容是否有问题

 

写入代码:

REPORT ztest MESSAGE-ID 00.

************************************************************************
* Tables Definitions
************************************************************************
TABLES: rlgrap.

************************************************************************
* Data Definitions                定义数据
************************************************************************
TYPES: BEGIN OF ty_file,
         line(1024) TYPE x,
       END OF ty_file.

DATA: gt_file TYPE TABLE OF ty_file.

DATA: gv_file_name TYPE sdbah-actid,
      gv_file_type TYPE sdbad-funct,
      gv_file      TYPE xstring.

************************************************************************
* Includes Module                 包含模块
************************************************************************

************************************************************************
* Selection Screen                选择屏幕
************************************************************************
PARAMETERS: p_file LIKE rlgrap-filename OBLIGATORY.

************************************************************************
* Initialization                  初始化事件
************************************************************************
INITIALIZATION.

************************************************************************
* At Selection Screen             PAI事件
************************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM frm_f4_file.

************************************************************************
* At Selection Screen Output      PBO事件
************************************************************************
AT SELECTION-SCREEN OUTPUT.

************************************************************************
* Report Format                   报表格式
************************************************************************
TOP-OF-PAGE.

END-OF-PAGE.

************************************************************************
* Main Process                    主要逻辑
************************************************************************
START-OF-SELECTION.

  "读取上传文件
  PERFORM frm_read_upload_file.

  "上传文件至SAP服务器
  PERFORM frm_upload_file_to_sap.

  cl_demo_output=>write( '1' ).
  cl_demo_output=>write( '2' ).
  cl_demo_output=>display(  ).

END-OF-SELECTION.

*&---------------------------------------------------------------------*
*& Form FRM_F4_FILE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_f4_file .
  CALL FUNCTION 'F4_FILENAME'
    IMPORTING
      file_name = p_file.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form FRM_READ_UPLOAD_FILE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_read_upload_file .

  DATA: lv_file_path   TYPE string,
        lv_file_length TYPE i,
        lv_file_name   TYPE dbmsgora-filename.

  lv_file_path = p_file.
  lv_file_name = p_file.

  CALL FUNCTION 'SPLIT_FILENAME'
    EXPORTING
      long_filename  = lv_file_name "上传文件路径
    IMPORTING
      pure_filename  = gv_file_name "文件名称
      pure_extension = gv_file_type. "文件后缀

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = lv_file_path
      filetype                = 'BIN'
    IMPORTING
      filelength              = lv_file_length
    TABLES
      data_tab                = gt_file
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      OTHERS                  = 17.
  IF sy-subrc <> 0.
    MESSAGE s001 WITH '上传文件失败' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.


ENDFORM.

*&---------------------------------------------------------------------*
*& Form FRM_UPLOAD_FILE_TO_SAP
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_upload_file_to_sap .

  DATA: ls_file TYPE ty_file.

  DATA: lv_folder TYPE eps2filnam.

  "指定目录和文件名
  lv_folder = '/hybris/FI/2023/202307/' && gv_file_name && '.' && gv_file_type.

  "在应用服务器打开文件(二进制模式)
  OPEN DATASET lv_folder FOR OUTPUT IN BINARY MODE.

  IF sy-subrc = 0.
    LOOP AT gt_file INTO ls_file.
      "向应用服务器上文件上写入数据
      TRANSFER ls_file TO lv_folder.
    ENDLOOP.

    "关闭应用服务器文件
    CLOSE DATASET lv_folder.

    MESSAGE s001 WITH '写入文件成功' DISPLAY LIKE 'S'.
    LEAVE LIST-PROCESSING.
  ELSE.
    MESSAGE s001 WITH '在应用服务器打开文件失败' DISPLAY LIKE 'E'.
    LEAVE LIST-PROCESSING.
  ENDIF.
ENDFORM.

 

读取效果:

 

读取代码:

TYPES: BEGIN OF ty_upload_file,
         file_name TYPE string,
         file_type TYPE string,
         file_size TYPE i,
         file      TYPE xstring,
       END OF ty_upload_file.

DATA: lt_upload_file TYPE TABLE OF ty_upload_file,
      ls_upload_file TYPE ty_upload_file.

DATA: lt_file   TYPE TABLE OF eps2fili,
      ls_file   TYPE eps2fili,
      lv_folder TYPE eps2filnam.

DATA: lt_binary TYPE TABLE OF rcgrepfile,
      ls_binary TYPE rcgrepfile.

DATA: lv_file_path TYPE dbmsgora-filename,
      lv_file_name TYPE sdbah-actid,
      lv_file_type TYPE sdbad-funct.

DATA: lv_msg      TYPE string,
      lv_xstring  TYPE xstring,
      lv_file_len TYPE i,
      lv_len      TYPE i.

"指定目录
lv_folder = '/hybris/FI/2023/202307/'.

"获取指定目录下所有的文件
CALL FUNCTION 'EPS2_GET_DIRECTORY_LISTING'
  EXPORTING
    iv_dir_name            = lv_folder
  TABLES
    dir_list               = lt_file
  EXCEPTIONS
    invalid_eps_subdir     = 1
    sapgparam_failed       = 2
    build_directory_failed = 3
    no_authorization       = 4
    read_directory_failed  = 5
    too_many_read_errors   = 6
    empty_directory_list   = 7
    OTHERS                 = 8.
IF sy-subrc <> 0.
  lv_msg  = '读取指定目录下的文件失败'.
ELSE.

  LOOP AT lt_file INTO ls_file WHERE name CS 'pdf' AND size > 0.

    CLEAR: lt_binary.

    "文件完整的路径
    lv_file_path = lv_folder && ls_file-name.

    "根据文件完整的路径获取文件名和后缀
    CALL FUNCTION 'SPLIT_FILENAME'
      EXPORTING
        long_filename  = lv_file_path "上传文件路径
      IMPORTING
        pure_filename  = lv_file_name "文件名称
        pure_extension = lv_file_type. "文件后缀

    "在应用服务器打开文件(二进制模式)
    OPEN DATASET lv_file_path FOR INPUT IN BINARY MODE.

    IF sy-subrc = 0.
      "按照2550长度读取文件,读到最后不足2550长度则跳出读取模式
      DO.
        CLEAR lv_len.
        READ DATASET lv_file_path INTO ls_binary LENGTH lv_len.
        IF sy-subrc <> 0.
          IF lv_len > 0.
            ADD lv_len TO lv_file_len.
          ENDIF.
          APPEND ls_binary TO lt_binary.
          EXIT.
        ENDIF.
        ADD lv_len TO lv_file_len.
        APPEND ls_binary TO lt_binary.
      ENDDO.
    ENDIF.

    "关闭应用服务器文件
    CLOSE DATASET lv_file_path.

    "二进制转XString
    CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
      EXPORTING
        input_length = lv_file_len
      IMPORTING
        buffer       = lv_xstring
      TABLES
        binary_tab   = lt_binary
      EXCEPTIONS
        failed       = 1
        OTHERS       = 2.

    IF sy-subrc = 0.
      ls_upload_file-file_name = lv_file_name.
      ls_upload_file-file_type = lv_file_type.
      ls_upload_file-file_size = xstrlen( lv_xstring ).
      ls_upload_file-file = lv_xstring.
      APPEND ls_upload_file TO lt_upload_file.
      CLEAR ls_upload_file.
    ENDIF.

  ENDLOOP.
ENDIF.

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

 

posted @ 2023-08-16 09:55  鲸与海  阅读(1664)  评论(0编辑  收藏  举报