I maintain the image file as an attachment for material in S/4hana's transaction code MM02:
You can read the binary contents of the picture file by using the following simple ABAP code:
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 ' + ', Lt_att type TABLE of Sibflporb. Call FUNCTION ' conversion_exit_matn1_input ' exporting INPUT = lv_id importing output = Lv_id.ls_appl_object-typei d = ' 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.
I then encapsulated the code in a function modulezdis_get_material_images, consuming the function module in Java code, and saving the binary content returned by S/4hana to the cost of the image file. In the ABAP system, the image binary file content type is rawstring, how do I use it in Java?
Use JCO to consume the ABAP function module code in Java:
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 (); } }
The code that holds the ABAP type rawstring field to save the cost to the file is encapsulated in method 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(); } }
After executing Java code, in the local C drive Temp directory can see the S/4hana material ID 16 of the two attachment picture:
To get more original Jerry's technical articles, please follow the public number "Wang Zixi" or scan the QR code below:
How to save a picture file stored in the S/4hana system with a Java program to a local