on 11-03-2014 3:39 PM
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();
}
}
}
======================================================================
Hi Rajesh
You mentioned that the decoded Base64 data is for an image file. However, you are using DOM processing which is for XML data. I suspect there might be an exception raised during DOM processing with the decoded image bytes, but it is not causing any mapping error because the exception handling is just printing the stacktrace.
To confirm this, I suggest you replace the e.printStackTrace() line with the following code. If there are errors during the mapping, this would cause the mapping to fail.
throw new StreamTransformationException("Exception: " + e.getMessage(), e);
Also, if the decoded data is the binary data for the image file, you can try to output the bytes directly to the output stream. Replace the DOM based logic with the logic below.
Old |
|
New |
|
Rgds
Eng Swee
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
81 | |
10 | |
10 | |
9 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.