cancel
Showing results for 
Search instead for 
Did you mean: 

Base64 encoded image string to image conversion in PO

manemahesh
Participant
0 Kudos

java-mapping-code.txtpayload-trigering-from-rwb.txt

Hi All,

I have a scenario from Legacy-->PO-->File.

Legacy system is sending base64 encoded string of image file to PO and PO has to decode it and send original image file to NFS location at target.

So for this requirement i developed a attached code which works fine in eclipse when we try to write a file on some location on local machine but when we deploy it in PO target image file which i get shows as blank.

My scenarion is getting executed successfully but image file which gets generated at target location is not the actual image which i am looking for. In message monitoring i can see the decoded bytecode in UTF-8 format after mapping step but not sure if this decoded UTF-8 code is creating issue to convert into actual image.

I have attached here the java code,sample payload which i am triggering from RWB.

Could you please help me if i am missing anything in code and if anyone knows why decodedbytes are not getting converted to actual image at the end.(Receiver is file adapter)

Regards

Mahesh

Accepted Solutions (1)

Accepted Solutions (1)

manemahesh
Participant
0 Kudos

Hi All,

I am now able to convert base64 encoded image string to image conversion with below code.I was initially trying to push the data without having parent node.So i created a parent node as <row> and then added child field inside it as <Base64Str> as below:-

<MT_Base64>

<row>

<Base64Str>

Below is the working java code:-

import java.io.InputStream; import java.io.OutputStream; import java.util.Base64; import javax.xml.bind.DatatypeConverter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.sap.aii.mapping.api.AbstractTransformation; import com.sap.aii.mapping.api.StreamTransformationException; import com.sap.aii.mapping.api.TransformationInput; import com.sap.aii.mapping.api.TransformationOutput; public class Decoder_V2 extends AbstractTransformation { @Override public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException { String encodedString = null; try{ //Read input and output payloads. InputStream inputStream = in.getInputPayload().getInputStream(); OutputStream outputStream = out.getOutputPayload().getOutputStream(); //Creates new instance of DOM tree builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //Creates new instance to obtain DOM document from xml. DocumentBuilder builder = factory.newDocumentBuilder(); //Obtain new DOM document from inputStream xml. Document doc = builder.parse(inputStream); NodeList children = doc.getElementsByTagName("row"); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if(node.getNodeType() == Node.ELEMENT_NODE){ Element eElement = (Element) node; encodedString = eElement.getElementsByTagName("Base64Str").item(0).getTextContent(); } } //Decode base64 string to byte code. byte[] bytesDecoded = Base64.getDecoder().decode(encodedString); //Derive output(target) xml message. outputStream.write(bytesDecoded); } catch (Exception exception) { getTrace().addDebugMessage(exception.getMessage()); throw new StreamTransformationException(exception.toString()); } } }

PFA Code in txt file.

Regards

Mahesh

decoder-v2.txt

Answers (1)

Answers (1)

apu_das2
Active Contributor
0 Kudos

Hi Mahesh,

You have to remove below before passing the encoded base 64 string to the decoder -

data:image/jpeg;base64,

Add below in your java mapping -

encodedString = encodedStringNode.getFirstChild().getNodeValue();

encodedString = encodedString.replace("data:image/jpeg;base64,","");


Thanks,

Apu

manemahesh
Participant
0 Kudos

Hi Apu,

We tried this but still its not working.Its just generating the output image as blank.

Regards

Mahesh