Skip to Content

issue with PDF upload to sharepoint

Dear Experts,

I have a file to sharepoint integration in which i need to upload PDF files to share point folder in base64 format.

I have done java mapping for base64 conversion but post conversion and upload, file is getting blank at sharepoint folder.

Assuming that the issue is with base64 conversion and file is corrupted post conversion as file size is same as processed file at sharepoint.

Please suggest on this.

Thanks in Advance!! :)

PS: I have tested the same using SOAPUI and are able to upload PDF file with file content.

I have inserted file using insert file as base64 option in SOAPUI.

Regards,

Gaurav

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Jun 28, 2017 at 12:56 PM

    Hi Gaurav!

    package com.evk.java.train;
    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.xml.bind.DatatypeConverter;
    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 BuildXMLWithBase64 extends AbstractTransformation {
    @Override
    public void transform(TransformationInput in, TransformationOutput out)throws StreamTransformationException {
    executeMapping(in.getInputPayload().getInputStream(), out.getOutputPayload().getOutputStream());
    }
    
    public void executeMapping(InputStream in, OutputStream out) throws StreamTransformationException {
    String xmlString = "";
    String XML_START_TAG = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    XML_START_TAG += "<ns:UploadPlainDocumentWithFolder xmlns:ns=\"http://schemas.abc.com/xyz/2010/11\">";
    String XML_END_TAG = "</ns:UploadPlainDocumentWithFolder>";
    
    xmlString = xmlString + XML_START_TAG + "<PlainDoc><DocumentName>" + "YourDocName" + "</DocumentName>";
    xmlString += "<DocumentURL>" + "http://yourURL" + "</DocumentURL>";
    
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    try {
    int nRead;
    byte[] data = new byte[16384];
    while ((nRead = in.read(data, 0, data.length)) != -1)
      buffer.write(data, 0, nRead);
    buffer.flush();
    }
    catch (Exception e) {
    throw new StreamTransformationException(e.getMessage());
    }
    
    xmlString += "<DocumentBytes>" + DatatypeConverter.printBase64Binary(buffer.toByteArray()) + "</DocumentBytes></PlainDoc>";
    xmlString += "<DocLibURL>http://xyz</DocLibURL><ApplicationId>APPL_ID</ApplicationId><UserId>USR_ID</UserId>" + XML_END_TAG;
    
    try {
    out.write(xmlString.getBytes("UTF-8"));
    }
    catch (Exception e) {
    throw new StreamTransformationException(e.getMessage());
    }
    }
    
    public static void main(String[] args) {
    try {
    FileInputStream inFile = new FileInputStream("C:\\Tests\\FCC_01.png");
    FileOutputStream outFile = new FileOutputStream("C:\\Tests\\FCC_01_out.xml");
    BuildXMLWithBase64 mapper = new BuildXMLWithBase64();
    mapper.executeMapping(inFile, outFile);
    
    outFile.flush();
    outFile.close();
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }
    
    }

    Output:

    Regards, Evgeniy.

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Evgeniy,

      Thank you so much. It has solved my problem of base64 conversion.

      I really appreciate your insights, and am looking forward to implementing many of your suggestions.

      Regards,

      Gaurav

  • Jun 22, 2017 at 10:26 AM

    Hi Gaurav,

    Are you sure that your Base64 class has the correct implementation? Does it come from a trusted source, like a standard SDK or Apache libraries?

    For example: org.apache.xml.security.utils.Base64;

    Regards,

    Andrzej

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Evgeniy, Andrzej,

      PFB code example using for base 64 conversion.

      Code:

      import java.io.*; //import com.oschrenk.io.Base64; import com.itextpdf.text.pdf.codec.Base64; import com.sap.aii.mapping.api.*; //import com.sap.aii.utilxi.base64.api.Base64; import org.apache.commons.codec.binary.Base64;

      public class base64Conversion extends AbstractTransformation { public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {

      try {

      String targetxml1 =""; String line =""; String source="";

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

      BufferedReader br = new BufferedReader( new InputStreamReader(ins));

      String XML_START_TAG = " http://schemas.abc.com/xyz/2010/11\">";

      String XML_END_TAG = "</ns:UploadPlainDocumentWithFolder>";

      targetxml1 = XML_START_TAG+ " "+ " abc.pdf "+ " http://xyz " + " ";

      String targetxml2="</ns:DocumentBytes>" + "</ns:PlainDoc>" + "<ns:DocLibURL>http://xyz</ns:DocLibURL>" + "<ns:ApplicationId>xyz</ns:ApplicationId>" + "<ns:UserId>gaurav</ns:UserId>" + XML_END_TAG;

      String base64="";

      out.getOutputPayload().getOutputStream().write(targetxml1.getBytes("UTF-8"));

      line = br.readLine();

      while (line != null)

      { source +=line;

      //byte[] str=source.getBytes();

      base64=Base64.encodeBytes(source.getBytes());

      line=br.readLine();

      }

      br.close();

      out.getOutputPayload().getOutputStream().write(base64.getBytes("UTF-8")); out.getOutputPayload().getOutputStream().write(targetxml2.getBytes("UTF-8"));

      }

      catch (Exception e) { throw new StreamTransformationException(e.getMessage()); } } }

      tags: PFB start and end tags as it is not appearing correctly in code post upload.

      Structure for uploading:

      Please correct/update if anything wrong with the code...

      Thanks alot in advance!!

      Regards,

      Gaurav

      str.png (7.8 kB)
      start.png (7.4 kB)
  • Jun 22, 2017 at 09:41 AM

    Gaurav,

    Have you tried comparing the RAW data being sent from SOAP UI with the content being sent from PI after execution of soap channel using TraceHTTP ?Do you see any difference in them ?

    Br,

    Manoj

    Add comment
    10|10000 characters needed characters exceeded