写点什么

使用 ABAP 代码返回 S/4HANA Material 上维护的 Attachment 明细

作者:Jerry Wang
  • 2021 年 11 月 22 日
  • 本文字数:2762 字

    阅读完需:约 9 分钟

使用ABAP代码返回S/4HANA Material上维护的Attachment明细

在事务码 MM02 里为 ID 为 16 的 material 维护附件:




如何使用 ABAP 代码获得如下附件的名称和文件内容?



REPORT zgos_api.
DATA ls_appl_object TYPE gos_s_obj.
DATA lo_gos_api TYPE REF TO cl_gos_api.
DATA lt_attachment_list TYPE gos_t_atta.
DATA lt_role_filter TYPE gos_t_rol.
DATA ls_attachment TYPE gos_s_atta.
DATA ls_attachm_cont TYPE gos_s_attcont.
DATA ls_atta_key TYPE gos_s_attkey.
DATA: lv_id TYPE matnr VALUE '16',
lt_att TYPE TABLE OF sibflporb.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = lv_id
IMPORTING
output = lv_id.
ls_appl_object-typeid = 'BUS1001006'.
ls_appl_object-instid = lv_id.
ls_appl_object-catid = 'BO'.
START-OF-SELECTION.
TRY.
lo_gos_api = cl_gos_api=>create_instance( ls_appl_object ).
APPEND cl_gos_api=>c_attachment TO lt_role_filter.
lt_attachment_list = lo_gos_api->get_atta_list( lt_role_filter ).
CATCH cx_gos_api INTO DATA(error).
WRITE:/ error->get_text( ).
RETURN.
ENDTRY.
DATA: ls_key TYPE gos_s_attkey.
LOOP AT lt_attachment_list ASSIGNING FIELD-SYMBOL(<id>).
ls_key-atta_id = <id>-atta_id.
ls_key-atta_cat = <id>-atta_cat.
DATA(ls) = lo_gos_api->get_al_item( ls_key ).
ENDLOOP.
复制代码


执行结果:



我在 S/4HANA 的事务码 MM02 里为 Material 维护图片文件作为附件:



通过如下简单的 ABAP 代码即可将图片文件的二进制内容读取出来:


REPORT zgos_api.
DATA ls_appl_object TYPE gos_s_obj.DATA lo_gos_api TYPE REF TO cl_gos_api.DATA lt_attachment_list TYPE gos_t_atta.DATA lt_role_filter TYPE gos_t_rol.
DATA: lv_id TYPE matnr VALUE '16', lt_att TYPE TABLE OF sibflporb.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT' EXPORTING input = lv_id IMPORTING output = lv_id.ls_appl_object-typeid = 'BUS1001006'.ls_appl_object-instid = lv_id.ls_appl_object-catid = 'BO'.
START-OF-SELECTION.
TRY. lo_gos_api = cl_gos_api=>create_instance( ls_appl_object ). APPEND cl_gos_api=>c_attachment TO lt_role_filter. lt_attachment_list = lo_gos_api->get_atta_list( lt_role_filter ). CATCH cx_gos_api INTO DATA(error). WRITE:/ error->get_text( ). RETURN. ENDTRY.
DATA: ls_key TYPE gos_s_attkey, lv_base64 type string.
LOOP AT lt_attachment_list ASSIGNING FIELD-SYMBOL(<id>). ls_key-atta_id = <id>-atta_id. ls_key-atta_cat = <id>-atta_cat. DATA(ls) = lo_gos_api->get_al_item( ls_key ). BREAK-POINT. ENDLOOP.
复制代码


然后我把这段代码封装到一个 Function moduleZDIS_GET_MATERIAL_IMAGES 里,在 Java 代码里消费这个 function module,把 S/4HANA 返回的二进制内容存成本地图片文件。在 ABAP 系统里图片二进制文件内容的类型为 RAWSTRING,如何在 Java 里使用它?



使用 JCO 在 Java 里消费 ABAP function module 的代码:


static private void getProductImageTest(){      JCoDestination destination = null;    try {      destination = JCoDestinationManager.getDestination(DESTINATION_NAME);      JCoRepository repo = destination.getRepository();        JCoFunction stfcConnection = repo.getFunction("ZDIS_GET_MATERIAL_IMAGES");
JCoParameterList imports = stfcConnection.getImportParameterList(); String materialID = "16";
imports.setValue("IV_MATERIAL_ID", materialID);
stfcConnection.execute(destination); JCoParameterList exports = stfcConnection.getExportParameterList(); int abapDuration = exports.getInt("EV_DURATION"); StringBuilder sb = new StringBuilder(); sb.append("{ \"" + ABAP_DURATION + "\": " + abapDuration + ","); sb.append("\"" + UPSELL_PRODUCT + "\":["); JCoTable codes = exports.getTable("ET_IMAGES"); int row = codes.getNumRows(); System.out.println("Total rows: " + row); System.out.println("ABAP duration: " + abapDuration); for( int i = 0; i < row; i++){ codes.setRow(i); sb.append("{\"" + FILE_ID + "\":" + codes.getString("FILEID") + "," + "\"" + FILE_OWNER + "\":\"" + codes.getString("OWNER") + "\"" + ","); sb.append("{\"" + FILE_CDATE + "\":" + codes.getString("CREATION_DATE") + "," + "\"" + FILE_NAME + "\":\"" + codes.getString("FILENAME") + "\""); storeLocalFile(codes); if( i < row - 1){ sb.append("},"); } else{ sb.append("}"); } } sb.append("]}"); System.out.println("Final json: " + sb.toString()); } catch (JCoException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
复制代码


其中把 ABAP 的类型为 RAWSTRING 的字段保存成本地文件的代码封装于方法 storeLocalFile 里:



static private void storeLocalFile(JCoTable codes){ InputStream is = codes.getBinaryStream("FILECONTENT"); try { File file = new File("c:\\temp\\" + codes.getString("FILENAME"));
byte[] bytes = new byte[is.available()]; is.read(bytes); OutputStream output = new FileOutputStream(file);
BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
bufferedOutput.write(bytes); bufferedOutput.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } }
复制代码


执行 Java 代码后,在本地 C 盘 temp 目录下能够看到 S/4HANA 里 Material ID 为 16 的两个附件图片:



要获取更多 Jerry 的原创技术文章,请关注公众号"汪子熙"。

发布于: 2021 年 11 月 22 日阅读数: 8
用户头像

Jerry Wang

关注

个人微信公众号:汪子熙 2017.12.03 加入

SAP成都研究院开发专家,SAP社区导师,SAP中国技术大使。

评论

发布
暂无评论
使用ABAP代码返回S/4HANA Material上维护的Attachment明细