AttachmentPart setDataHandler() Method



Description

The Javax.xml.soap.AttachmentPart.setDataHandler(DataHandler dataHandler) method sets the given DataHandler object as the data handler for this AttachmentPart object. Typically, on an incoming message, the data handler is automatically set. When a message is being created and populated with content, the setDataHandler method can be used to get data from various data sources into the message.

Declaration

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

public abstract void setDataHandler(DataHandler dataHandler)

Parameters

dataHandler − the DataHandler object to be set

Return Value

This method does not return a value.

Exception

IllegalArgumentException − if there was a problem with the specified DataHandler object.

Example

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

package com.tutorialspoint;

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();

         // set the handler
         attachPart.setDataHandler(handler);

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

         // get the content type
         System.out.println("" + attachPart.getDataHandler().getContentType());
         
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

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

HandlerHeader
javax_xml_soap_attachmentpart.htm
Advertisements