這期內(nèi)容當中小編將會給大家?guī)碛嘘P如何將S/4HANA系統(tǒng)存儲的圖片文件用Java程序保存到本地,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)-云計算及IDC服務提供商,涵蓋公有云、IDC機房租用、資陽托管服務器、等保安全、私有云建設等企業(yè)級互聯(lián)網(wǎng)基礎服務,歡迎來電:028-86922220
我在S/4HANA的事務碼MM02里為Material維護圖片文件作為附件:
通過如下簡單的ABAP代碼即可將圖片文件的二進制內(nèi)容讀取出來:
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(). ls_key-atta_id = -atta_id. ls_key-atta_cat = -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返回的二進制內(nèi)容存成本地圖片文件。在ABAP系統(tǒng)里圖片二進制文件內(nèi)容的類型為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(); } }
執(zhí)行Java代碼后,在本地C盤temp目錄下能夠看到S/4HANA里Material ID為16的兩個附件圖片:
上述就是小編為大家分享的如何將S/4HANA系統(tǒng)存儲的圖片文件用Java程序保存到本地了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。