AttachmentPart getContent() Method



Description

The Javax.xml.soap.AttachmentPart.getContent() method gets the content of this AttachmentPart object as a Java object. The type of the returned Java object depends on (1) the DataContentHandler object that is used to interpret the bytes and (2) the Content-Type given in the header.

For the MIME content types "text/plain", "text/html" and "text/xml", the DataContentHandler object does the conversions to and from the Java types corresponding to the MIME types. For other MIME types,the DataContentHandler object can return an InputStream object that contains the content data as raw bytes.

Declaration

Following is the declaration for javax.xml.soap.AttachmentPart.getContent() method

public abstract Object getContent()

Parameters

NA

Return Value

This method returns a Java object with the content of this AttachmentPart object

Exception

SOAPException − if there is no content set into this AttachmentPart object or if there was a data transformation error.

Example

The following example shows the usage of javax.xml.soap.AttachmentPart.getContent() method.

package com.tutorialspoint;

import java.util.Iterator;
import javax.activation.DataHandler;
import javax.xml.soap.*;

public class AttachmentPartDemo {

   public static void main(String[] args) {
      try {

         // create a new SOAPMessage
         SOAPMessage message = MessageFactory.newInstance().createMessage();

         // create a string as a new attachment 
         String attachment = "This is an attachment";

         // get the data handler and assign the attachment
         DataHandler handler = new DataHandler(attachment, "HandlerHeader");

         // create the attachment part
         AttachmentPart attachPart = message.createAttachmentPart(handler);

         // add content in the attachment
         attachPart.setContent("Attachment Content", "plain/text");

         // add the attachment part in the message
         message.addAttachmentPart(attachPart);

         // get the content of attachment part
         System.out.println("" + attachPart.getContent());


      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

If we compile the code and execute it, this will produce the following result −

Attachment Content
javax_xml_soap_attachmentpart.htm
Advertisements