AttachmentPart setMimeHeader() Method



Description

The Javax.xml.soap.AttachmentPart.setMimeHeader(String name, String value) method changes the first header entry that matches the given name to the given value, adding a new header if no existing header matches. This method also removes all matching headers but the first.

Declaration

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

public abstract void setMimeHeader(String name,String value)

Parameters

  • name − a String giving the name of the header for which to search

  • value − a String giving the value to be set for the header whose name matches the given name

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

         // set content-Type a new value
         attachPart.setMimeHeader("Content-type", "New Value");

         // print the mime header
         System.out.println("" + attachPart.getMimeHeader("Content-type")[0]);

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

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

New Value
javax_xml_soap_attachmentpart.htm
Advertisements