cancel
Showing results for 
Search instead for 
Did you mean: 

Image error - ABAP Proxy to SFTP

Former Member
0 Kudos

Hello SAP fellowship,

I've got a problem with my scenario. I'm intending to save a JPG file in a server. The scenario works fine except for one thing. In the end of the execution the file I've got in my server is an XML and not a JPG. I don't know if i'm forgetting some module or what. I've implemented ASMA in order to solve it but it seems it wasn't enought.

Thanks in advance.

Pablo

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Dear Pablo,


1) To make the mapping Filename should use a user function (UDF) try to view these post:

http://wiki.scn.sap.com/wiki/pages/viewpage.action?original_fqdn=wiki.sdn.sap.com&pageId=272171407


http://scn.sap.com/community/pi-and-soa-middleware/blog/2009/03/26/dynamic-configuration-vs-variable...


Example:

2) Then to convert the contents of the tag "Imagen" you have to create a java program that extracts the contents of the Base64Binary image using Java Mapping and write it directly in place of the XML payload.


Try with this:

http://scn.sap.com/people/farooq.farooqui3/blog/2008/05/22/decode-base64-incoming-encoded-informatio...


This help to Read XML File In Java

http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/


It may also be useful

http://scn.sap.com/thread/2068827#


Example, The following code works fine:

package javamapping;

import com.sap.aii.mapping.api.*;

import java.io.*;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document;

import org.w3c.dom.NodeList;

import sun.misc.BASE64Decoder;

public class JMDecodeImage extends AbstractTransformation

{

  private MappingTrace trace;

  public void transform (TransformationInput in, TransformationOutput out)

  throws StreamTransformationException

  {  

  trace = getTrace();

  byte[] imageByte;

  InputStream ins = in.getInputPayload().getInputStream();

  try{//Get document XML from input stream.

  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

  DocumentBuilder builder = factory.newDocumentBuilder();

  Document docOld = builder.parse(ins);

  docOld.getDocumentElement().normalize();

//Get the node List that contains the node "File-Name" and "Image"..

  NodeList details = docOld.getChildNodes();

//Get the Text of Node "Image" Item(0).   

  String data = details.item(0).getChildNodes().item(0).getTextContent();   

//Instantiate an object of BASE64Decoder...   

  BASE64Decoder decoder =  new BASE64Decoder();   

//Decode the encoded information....   

  imageByte = decoder.decodeBuffer(data);  

//Write Output Pay-load

  out.getOutputPayload().getOutputStream().write(imageByte);

  }

  catch (Exception e) {  trace.addWarning(e.getMessage());  }

  }

}



Best regards,

Patricio Cabezas

0 Kudos

Operation Mapping looks like this:

Answers (1)

Answers (1)

Harish
Active Contributor
0 Kudos

Hi Pablo,

How your are sending the JPG image from Proxy? are you sending as attachment?

Please provide more detail about the schenario and error.

regards,

Harish

Former Member
0 Kudos

Hello Harish,

Thank you for your answer.

The image is included in a field type Base64Binary. The proxy has 2 fields:

<filename>String</filename>

<image>Base64Binary</image>

Regards

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Pablo,

Extract the contents of the Base64Binary image using Java Mapping and write it directly in place of the XML payload (SFTP receiver does not support attachments). You can test the output by following this blog

Hope this helps,

Mark

Former Member
0 Kudos

Hi Mark,

I tried to do the "Mapping" as follows, but the problem is that the received file saves it as text.

then I detail the steps performed.

Program "Java Mapping":

import com.sap.aii.mapping.api.*;

import java.io.*;

public class MapBinaryToHexStream extends AbstractTransformation

{

  private MappingTrace trace;

  private String digits = "0123456789ABCDEF"; 

  public void transform (TransformationInput in, TransformationOutput out)

  throws StreamTransformationException

  {

  // Initialize variables, objects for processing

  trace = getTrace();

  BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out.getOutputPayload().getOutputStream())); 

  StringBuffer sb = new StringBuffer();

  int c;

  byte[] buffer = new byte[4096]; 

  // Get buffered reader for input stream

  InputStream ins = in.getInputPayload().getInputStream();

  // Read direct binary file input stream and convert to hex

  try { 

  while(ins.read(buffer) != -1) 

  { 

  for(int i = 0; i < buffer.length; i++) 

  { 

  c = (int) buffer[i] & 0xff; 

  char bit1 = digits.charAt(c >> 4); 

  char bit2 = digits.charAt(c & 0xf); 

  sb.append(Character.toString(bit1) + Character.toString(bit2)); 

  } 

  } 

  }

  catch (IOException e) { 

  trace.addWarning(e.getMessage()); 

  }

  // Close XML message stream 

  try { 

  bw.write(sb.toString());

  bw.close(); 

  }

  catch (IOException e) { 

  trace.addWarning(e.getMessage()); 

  } 

  }

}

The Result is a File like this : when it should be an image (. JPG or. PDF)