Hi Dear friends,
I am trying to decode Base64 string present in given xml file and write the decoded file in image format in ftp folder. Sender & receiver channels are dummy and I have used the Java mapping code given in this link - http://scn.sap.com/thread/3475411 by Indrajit.
The issue is that the images in target FTP are of 0kb size, and cannot be opened via editor. It seems they are not valid ones. I tried to insert Trace into code to check whether the contents of TAG - 'ship:GraphicImage' is getting picked or not, this tag value is being decoded. The value to be decoded is getting picked properly but the output is not valid. No encoding-decofding specific settings are done in channels.
The JDK is 1.5, commons-codec-1.6.jar.Can you please suggest what may be the issue ?
==========================================
package com.sap.base64toImage;
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.codec.binary.Base64;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import com.sap.aii.mapping.api.AbstractTrace;
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 Xmltoimagebase64 extends AbstractTransformation {
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
AbstractTrace trace = getTrace();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document docOld = builder.parse(arg0.getInputPayload().getInputStream());
NodeList details = docOld.getElementsByTagName("ship:GraphicImage");
String data = details.item(0).getChildNodes().item(0).getNodeValue();
trace.addInfo("data value is -----" + data);
byte[] decodedBytes = Base64.decodeBase64(data.getBytes());
trace.addInfo("decoded base64 byte array is ------" + decodedBytes);
Document docNew = builder.parse(new ByteArrayInputStream(decodedBytes));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(docNew);
StreamResult result = new StreamResult(arg1.getOutputPayload().getOutputStream());
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
======================================================================