AttachmentPart addMimeHeader() Method



Description

The Javax.xml.soap.AttachmentPart.addMimeHeader(String name, String value) method adds a MIME header with the specified name and value to this AttachmentPart object. Note that RFC822 headers can contain only US-ASCII characters.

Declaration

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

public abstract void addMimeHeader(String name, String value)

Parameters

  • name − a String giving the name of the header to be added

  • value − a String giving the value of the header to be added

Return Value

This method does not return a value.

Exception

IllegalArgumentException − if there was a problem with the specified mime header name or value

Example

The following example shows the usage of javax.xml.soap.AttachmentPart.addMimeHeader() 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(handler);

         // add a MIMEHeader
         attachPart.addMimeHeader("Content-type", "plain/text");

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

         // get the headers of the attachment part
         String[] a = attachPart.getMimeHeader("Content-type");
         for (int i = 0; i < 2; i++) {
            System.out.println("" + a[i]);
         }

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

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

HandlerHeader
plain/text
javax_xml_soap_attachmentpart.htm
Advertisements