background image

Running Attachments

<< Creating an AttachmentPart Object | Running SOAPFaultTest >>
<< Creating an AttachmentPart Object | Running SOAPFaultTest >>
A
TTACHMENTS
.
JAVA
167
After it reads input from a file into a string named
stringContent
, it sets the
content of the attachment to the value of the string and the type to
text/plain
and also sets a content ID.
attachment1.setContent(stringContent, "text/plain");
attachment1.setContentId("attached_text");
It then adds the attachment to the message:
message.addAttachmentPart(attachment1);
The example uses a
javax.activation.DataHandler
object to hold a reference
to the graphic that constitutes the second attachment. It creates this attachment
using the form of the
createAttachmentPart
method that takes a
DataHandler
argument.
// Create attachment part for image
URL url = new URL("file:///../xml-pic.jpg");
DataHandler dataHandler = new DataHandler(url);
AttachmentPart attachment2 =
message.createAttachmentPart(dataHandler);
attachment2.setContentId("attached_image");
message.addAttachmentPart(attachment2);
The example then retrieves the attachments from the message. It displays the
contentId
and
contentType
attributes of each attachment and the contents of
the text attachment.
Running Attachments
To run Attachments, you use the file
build.xml
that is in the directory
<INSTALL>/javaeetutorial5/examples/saaj/attachments/
.
To run Attachments, use the following command:
asant run -Dfile=path_name
Specify any text file as the
path_name
argument. The
attachments
directory
contains a file named
addr.txt
that you can use:
asant run -Dfile=addr.txt