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 ="+"; Imports.SetValue("iv_material_id", materialid); Stfcconnection.Execute(destination); Jcoparameterlist exports = stfcconnection.getexportparameterlist();intAbapduration = exports.getInt("Ev_duration"); StringBuilder SB =NewStringBuilder (); Sb.Append("{ \""+ Abap_duration +"\": "+ Abapduration +","); Sb.Append("\""+ Upsell_product +"\":["); Jcotable codes = exports.getTable("Et_images");introw = codes.getnumrows(); System. out.println("Total rows:"+ row); System. out.println("ABAP Duration:"+ abapduration); for(inti =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 blockE.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 =NewFile ("C:\\Temp\\"+ Codes.getString("FILENAME"));byte[] bytes =New byte[IS.available()]; Is.Read(bytes); OutputStream output =NewFileOutputStream (file); Bufferedoutputstream Bufferedoutput =NewBufferedoutputstream (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