如何將S/4HANA系統儲存的圖片檔案用Java程式儲存到本地

來源:互聯網
上載者:User

標籤:原創   red   string   取出   err   ota   圖片   loop   creat   

我在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的原創技術文章,請關注公眾號"汪子熙"或者掃描下面二維碼:

如何將S/4HANA系統儲存的圖片檔案用Java程式儲存到本地

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.