Class AttachmentPart

java.lang.Object
jakarta.xml.soap.AttachmentPart

public abstract class AttachmentPart extends Object
A single attachment to a SOAPMessage object. A SOAPMessage object may contain zero, one, or many AttachmentPart objects. Each AttachmentPart object consists of two parts, application-specific content and associated MIME headers. The MIME headers consists of name/value pairs that can be used to identify and describe the content.

An AttachmentPart object must conform to certain standards.

  1. It must conform to MIME [RFC2045] standards
  2. It MUST contain content
  3. The header portion MUST include the following header:
    • Content-Type
      This header identifies the type of data in the content of an AttachmentPart object and MUST conform to [RFC2045]. The following is an example of a Content-Type header:
             Content-Type:  application/xml
             
      The following line of code, in which ap is an AttachmentPart object, sets the header shown in the previous example.
             ap.setMimeHeader("Content-Type", "application/xml");
             

There are no restrictions on the content portion of an AttachmentPart object. The content may be anything from a simple plain text object to a complex XML document or image file.

An AttachmentPart object is created with the method SOAPMessage.createAttachmentPart. After setting its MIME headers, the AttachmentPart object is added to the message that created it with the method SOAPMessage.addAttachmentPart.

The following code fragment, in which m is a SOAPMessage object and contentStringl is a String, creates an instance of AttachmentPart, sets the AttachmentPart object with some content and header information, and adds the AttachmentPart object to the SOAPMessage object.

     AttachmentPart ap1 = m.createAttachmentPart();
     ap1.setContent(contentString1, "text/plain");
     m.addAttachmentPart(ap1);
 

The following code fragment creates and adds a second AttachmentPart instance to the same message. jpegData is a binary byte buffer representing the jpeg file.

     AttachmentPart ap2 = m.createAttachmentPart();
     byte[] jpegData =  ...;
     ap2.setContent(new ByteArrayInputStream(jpegData), "image/jpeg");
     m.addAttachmentPart(ap2);
 

The getContent method retrieves the contents and header from an AttachmentPart object. Depending on the DataContentHandler objects present, the returned Object can either be a typed Java object corresponding to the MIME type or an InputStream object that contains the content as bytes.

     String content1 = ap1.getContent();
     java.io.InputStream content2 = ap2.getContent();
 
The method clearContent removes all the content from an AttachmentPart object but does not affect its header information.
     ap1.clearContent();
 
Since:
1.6