cancel
Showing results for 
Search instead for 
Did you mean: 

base64 decode from XML

Former Member
0 Kudos

Hello,

I'm trying to decode base64 data from an XMLvia User defined function. In the UDF I decoded and wrote it as a file, but the decoded PDF appears corrupt.

Can anyone advise whats wrong here or how this could be done better?

Please advise. Thank you.

Larry.

XML with base64 data:

<SpecialInstructions InstructionType="Instructions">

***Base64 data****

</SpecialInstructions>

***Base64 data**** -> Mapped to var1 of the UDF.

// Import com.sap.engine.lib.xml.util.BASE64Decoder

String f2e =  new String(BASE64Decoder.decode(var1.getBytes()));

AbstractTrace trace = container.getTrace();

String location = "/********/test/pdf/";

String fileName = "test1.pdf";

try {

FileWriter fwriter = new FileWriter(location+fileName);

BufferedWriter out = new BufferedWriter(fwriter);

out.write(f2e);

out.close();

} catch (IOException e) {

            trace.addWarning("Unable to write file :");

        } finally{

            trace.addWarning("File Written");

        }

return "success";

Accepted Solutions (1)

Accepted Solutions (1)

former_member181985
Active Contributor
0 Kudos

Hi Aber,

>>String f2e =  new String(BASE64Decoder.decode(var1.getBytes()));

You should declare this as byet[] to treat data as binary instead of string (textual)


byte[] f2e = BASE64Decoder.decode(var1.getBytes());



Best Regards,

Praveen Gujjeti

Former Member
0 Kudos

Thanks Praveen, If I treat that as byte what should I use to write the file in this case as BufferedWriter expects a string, any idea?

former_member181985
Active Contributor
0 Kudos

You can use,

FileOutputStream fos = new FileOutputStream(new File(location+fileName));

fos.write(f2e); //f2e is byte array

Former Member
0 Kudos

Thanks again, that did the trick. I will mark it answered, but just a quick question, is there a chance I can use the regular file channel to write this file as a PDF? I have some archiving functions in the adapter module and I need to archive this file written in a different process which is already built in that adapter module..

former_member181985
Active Contributor
0 Kudos

Please brief about your scenario flow with technical adapters involved. Probably, then I could provide a better a solution

Sender Adapter? --> PI/PO --> Receiver Adapter?

Best Regards,

Praveen Gujjeti

Former Member
0 Kudos

I'm receiving an order create XML file with PDF encoded(as base64) in the special instructions tag via AS2. The file adapter reads the file and the order details from the XML will be mapped to an IDoc and send to R/3. The PDF needs to decoded and archived in the Seeburger Message tracking database.(via Filestore module as short term archive). The PDF needs to be send to R/3 and it will be loaded as attachment to the sales order.

My plan is to decode the PDF and write it as file to store it in the message tracking database and sending it to the R/3 filesystem so that it can be picked up and loaded in to the Order later.

Let me know if you have better ideas, Thank you.

former_member181985
Active Contributor
0 Kudos

If I have understood your requirement correct

File Adapter (XML file with base64 pdf and order details) --> PI --> IDOC Order XML to R/3 using IDOC adapter

You can have to receivers for above scenario with a split mapping 1) IDOC 2) File system

For File System receiver you could use a java mapping to read base64 data and then decode to write it to outputstream (TransformationOutput) and the rest file adapter will take care. You can also a use graphical mapping with UDF to create PDF attachment and then use standard SAP payload swapbean in File receiver channel to swap main payload with PDF attachment

Best Regards,

Praveen Gujjeti

Former Member
0 Kudos

Great, I just read through one of your previous answer to the same context and tried the attachment and it worked. Thank you for your help.

venkatagiri_gongadi
Participant
0 Kudos

Hi Praveen,

I have a same scenario,could you please provide me UDF for this.

Cheers,

Giri

Former Member
0 Kudos

Giri,

here is the extract that might help.

import - sap.engine.lib.xml.util.BASE64Decoder

******

   GlobalContainer globalContainer = container.getGlobalContainer();
   OutputAttachments oa = globalContainer.getOutputAttachments();
   Attachment attach = oa.create(FileName, MimeType, BASE64Decoder.decode(Base64Data.getBytes()));
  

oa.setAttachment(attach);

*****

Larry.

Answers (0)