获取附件
...
获取附件
将SAP采购申请上(其他附件同理)的附件传输给外围系统
将文件转换成二进制再转换成16进制(XSTRING)传输给外围系统
过程
可以在TOJTT表中查找相关对象类型
可以在SRGBTBREL表中查看已存在附件
可以研究一下能否直接使用ODATA(CV_ATTACHMENT还不会用ODATA🐤)
代码
自己写了(抄🎧)个公开类-SE24:ABAP类构建器
METHOD GET_PR_ATTACHMENTS.
DATA: LV_OBJECT_TYPE TYPE DOKOB,
LV_OBJECT_TYPE_LONG TYPE OBJECTTYPEID,
LV_SEMANTIC_OBJECT TYPE /UI2/SEMANTIC_OBJECT,
LV_OBJECT_KEY TYPE OBJKY,
LT_ATTACHMENTS TYPE TT_ODATA_CV_ATTACH,
LS_ORIGINAL_CONTENT TYPE CL_CV_ATTACHMENT_MPC_EXT=>TS_ORIGINALCONTENT_EXT,
LT_ORIGINAL_CONTENTS TYPE CL_CV_ATTACHMENT_MPC_EXT=>TT_ORIGINALCONTENT_EXT,
LT_GOS_ORIGINAL_CONT TYPE CL_ODATA_CV_BO=>TY_T_ATTACHMENT_CONTENT,
LS_GOS_ORIGINAL_CONT TYPE CL_ODATA_CV_BO=>TY_S_ATTACHMENT_CONTENT,
LX_CV_BASE TYPE REF TO CX_ODATA_CV_BASE_EXCEPTION,
LT_RETURN TYPE BAPIRET2_T,
LT_HEADERS TYPE TIHTTPNVP,
LO_PERS_SERV TYPE REF TO IF_ODATA_CV_SERVICE,
LB_ATTACH_PERSIST TYPE REF TO BADI_CV_ODATA_ATTACH_PERSIST,
LS_ODATA_CV_OBJ_MAP TYPE ODATA_CV_OBJ_MAP.
DATA LV_OBJ_ID TYPE SX_OBJ_ID.
DATA LS_CONTENT TYPE SX_CONTENT.
DATA LT_DATA TYPE TABLE OF SOLIX.
DATA LT_DATA2 TYPE TABLE OF SOLI.
DATA LV_FILENAME TYPE STRING.
DATA LV_PATH TYPE STRING.
DATA LV_FULLPATH TYPE STRING.
DATA LV_LEN TYPE I.
DATA GS_ITEM TYPE ZTWOSTRING.
DATA LV_FILE_LENGTH TYPE I.
DATA LV_STRING_TABLE TYPE XSTRING.
DATA LV_STRING_BSE64 TYPE STRING.
DATA LV_EF_HASHSTRING TYPE STRING.
DATA LV_STRING TYPE STRING.
**********************************************************************
LV_OBJECT_TYPE_LONG = LV_OBJECT_TYPE = 'BUS2105'. "Business Object for Purchase Requisition
LV_OBJECT_KEY = IV_BANFN. "Purchase Requisition Number
**********************************************************************
GET BADI LB_ATTACH_PERSIST
FILTERS
OBJECT_TYPE = LV_OBJECT_TYPE
SEMANTIC_OBJECT = LV_SEMANTIC_OBJECT.
IF LB_ATTACH_PERSIST IS BOUND.
CALL BADI LB_ATTACH_PERSIST->FINALIZE_OBJECT_KEYS
EXPORTING
IV_OBJECTTYPE = LV_OBJECT_TYPE
IV_OBJECTTYPE_LONG = LV_OBJECT_TYPE_LONG
CHANGING
CV_FINALIZED_OBJECTKEY = LV_OBJECT_KEY.
ENDIF.
**********************************************************************
TRY.
LS_ODATA_CV_OBJ_MAP-GOS_OBJTYPE = LV_OBJECT_TYPE.
LO_PERS_SERV = CL_CV_ATTACHMENT_OBJ_FACTORY=>GET_OBJECT_INSTANCE(
IV_PERS_OBJECTTYPE = IF_ODATA_CV_CONSTANTS=>GC_GOS_OBJECT
IS_OBJ_MAP = LS_ODATA_CV_OBJ_MAP ).
CALL METHOD LO_PERS_SERV->GET_ATTACHMENT_LIST
EXPORTING
IV_OBJECTKEY = LV_OBJECT_KEY
IV_OBJECTTYPE_LONG = LV_OBJECT_TYPE_LONG
IMPORTING
ET_ORIGINAL_CONTENT = LT_GOS_ORIGINAL_CONT
ET_HEADER = LT_HEADERS
ET_MESSAGES = LT_RETURN.
IF LT_RETURN IS INITIAL.
MOVE-CORRESPONDING EXACT LT_GOS_ORIGINAL_CONT TO LT_ATTACHMENTS.
LOOP AT LT_GOS_ORIGINAL_CONT INTO LS_GOS_ORIGINAL_CONT.
READ TABLE LT_ATTACHMENTS WITH KEY APPLICATION_ID = LS_GOS_ORIGINAL_CONT-APPLICATION_ID
FILE_ID = LS_GOS_ORIGINAL_CONT-FILE_ID
TRANSPORTING NO FIELDS.
IF SY-SUBRC <> 0.
DELETE LT_GOS_ORIGINAL_CONT USING KEY LOOP_KEY.
ENDIF.
ENDLOOP.
IF LT_GOS_ORIGINAL_CONT IS NOT INITIAL.
CLEAR LT_ORIGINAL_CONTENTS.
LOOP AT LT_GOS_ORIGINAL_CONT INTO LS_GOS_ORIGINAL_CONT.
CLEAR LS_ORIGINAL_CONTENT.
MOVE-CORRESPONDING LS_GOS_ORIGINAL_CONT TO LS_ORIGINAL_CONTENT.
LS_ORIGINAL_CONTENT-CONTENTTYPE = LS_GOS_ORIGINAL_CONT-MIMETYPE.
LS_ORIGINAL_CONTENT-FULLNAME = LS_GOS_ORIGINAL_CONT-CREATEDBY_FULLNAME.
LS_ORIGINAL_CONTENT-ATTACHMENTSTATUS = LS_GOS_ORIGINAL_CONT-ATTACHMENT_STATUS.
LS_ORIGINAL_CONTENT-CONTENTSOURCE = LS_GOS_ORIGINAL_CONT-ATTACHMENT_URL.
LS_ORIGINAL_CONTENT-SOURCE = IF_ODATA_CV_CONSTANTS=>GC_GOS_OBJECT.
APPEND LS_ORIGINAL_CONTENT TO LT_ORIGINAL_CONTENTS.
ENDLOOP.
ENDIF.
ENDIF.
CATCH CX_ODATA_CV_DB_EXCEPTION CX_ODATA_CV_BASE_EXCEPTION INTO LX_CV_BASE.
ENDTRY.
**********************************************************************
LOOP AT LT_ORIGINAL_CONTENTS ASSIGNING FIELD-SYMBOL(<FILES>).
"Get file content
LV_OBJ_ID = <FILES>-DOCUMENTNUMBER.
CALL FUNCTION 'SO_OBJECT_DATA_FOR_SEND_GET'
EXPORTING
OBJECT_ID = LV_OBJ_ID
IMPORTING
CONTENT = LS_CONTENT
EXCEPTIONS
ERR_OBJ_NOT_FOUND = 1
ERR_NO_AUTHORIZATION = 2
ERR_WRONG_REFERENCE = 3
OTHERS = 4.
LT_DATA = LS_CONTENT-LINESX.
LV_LEN = LS_CONTENT-LEN.
* "Can download to local
* CALL METHOD cl_gui_frontend_services=>file_save_dialog
* CHANGING
* filename = lv_filename
* path = lv_path
* fullpath = lv_fullpath
* EXCEPTIONS
* cntl_error = 1
* error_no_gui = 2
* not_supported_by_gui = 3
* OTHERS = 4.
*
* CALL FUNCTION 'GUI_DOWNLOAD'
* EXPORTING
* bin_filesize = lv_len
* filename = lv_fullpath
* filetype = 'BIN'
* TABLES
* data_tab = lt_data.
* LV_FILE_LENGTH = LINES( LT_DATA[] ) * 255.
LV_FILE_LENGTH = LV_LEN.
*将二进制数据转换为XSTRING格式
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
INPUT_LENGTH = LV_FILE_LENGTH
IMPORTING
BUFFER = LV_STRING_TABLE
TABLES
BINARY_TAB = LT_DATA
EXCEPTIONS
FAILED = 1
OTHERS = 2.
**XSTRING转BASE64
* CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'
* EXPORTING
* INPUT = LV_STRING_TABLE
* IMPORTING
* OUTPUT = LV_STRING_BSE64.
**将base64进行sha256加密变短点太长了,不太行这个加密好像不能解密
* TRY.
* CL_ABAP_MESSAGE_DIGEST=>CALCULATE_HASH_FOR_CHAR(
* EXPORTING
* IF_ALGORITHM = 'SHA256'
* IF_DATA = LV_STRING_BSE64
* IMPORTING
* EF_HASHSTRING = LV_EF_HASHSTRING
* ).
* CATCH CX_ROOT.
* " Eh, what're you gonna do?
* ENDTRY.
IF SY-SUBRC = 0 AND LV_STRING_TABLE IS NOT INITIAL.
GS_ITEM-FILENAME = <FILES>-FILENAME.
GS_ITEM-STRING_TABLE = LV_STRING_TABLE.
* GS_ITEM-STRING_TABLE = LV_EF_HASHSTRING.
* GS_ITEM-STRING_TABLE = LV_STRING_BSE64.
APPEND GS_ITEM TO GT_ITEM.
ENDIF.
**将二进制数据转换为STRING格式
* CALL FUNCTION 'SCMS_BINARY_TO_STRING'
* EXPORTING
* INPUT_LENGTH = LV_FILE_LENGTH
** FIRST_LINE = 0
** LAST_LINE = 0
** MIMETYPE = ' '
** ENCODING =
* IMPORTING
* TEXT_BUFFER = LV_STRING
** OUTPUT_LENGTH =
* TABLES
* BINARY_TAB = LT_DATA
** EXCEPTIONS
** FAILED = 1
** OTHERS = 2
* .
*
* IF SY-SUBRC = 0 AND LV_STRING IS NOT INITIAL.
* GS_ITEM-FILENAME = <FILES>-FILENAME.
* GS_ITEM-STRING_TABLE = LV_STRING.
* APPEND GS_ITEM TO GT_ITEM.
* ENDIF.
IF LS_CONTENT-LINES[] IS NOT INITIAL . ".txt?这里好像直接就是文本
* LT_DATA2 = LS_CONTENT-LINES.
* LV_LEN = LS_CONTENT-LEN.
* LV_FILE_LENGTH = LINES( LT_DATA2[] ).
*
**将二进制数据转换为XSTRING格式
* CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
* EXPORTING
* INPUT_LENGTH = LV_FILE_LENGTH
* IMPORTING
* BUFFER = LV_STRING_TABLE
* TABLES
* BINARY_TAB = LT_DATA2
* EXCEPTIONS
* FAILED = 1
* OTHERS = 2.
*
* IF SY-SUBRC = 0 AND LV_STRING_TABLE IS NOT INITIAL.
* GS_ITEM-FILENAME = <FILES>-FILENAME.
* GS_ITEM-STRING_TABLE = LV_STRING_TABLE.
* APPEND GS_ITEM TO GT_ITEM.
* ENDIF.
ENDIF.
ENDLOOP.
ENDMETHOD.
参考文献
运用GOS技术将报文中的附件(PDF.TXT等) 存至系统附件内-Ramos丶
ABAP - 获取PR采购申请/PO订单等标准对象上的附件(GOS)-14年ABAP码农
你认为这篇文章怎么样?
- 0
- 0
- 0
- 0
- 0
- 0