uCosminexus Application Server, Web Service Development Guide
This subsection describes how to create a Java object depending on the data to be sent as an attachment in the MTOM/XOP format.
You can send an existing text file by using either of the two methods such as byte[] or javax.activation.DataHandler. The details on the respective procedures are as follows:
The procedure to send an existing text file by using byte[] is as follows:
java.io.FileInputStream fileInputStream =
new java.io.FileInputStream("sample.txt");
|
java.io.ByteArrayOutputStream byteArrayOutputStream =
new java.io.ByteArrayOutputStream();
int i = 0;
while ((i = fileInputStream.read()) != -1) {
byteArrayOutputStream.write(i);
}
byte[] bytes = byteArrayOutputStream.toByteArray();
|
The procedure to attach and send an existing file by using javax.activation.DataHandler is as follows:
javax.activation.FileDataSource fileDataSource =
new javax.activation.FileDataSource("sample.txt");
|
javax.activation.DataHandler dataHandler =
new javax.activation.DataHandler(fileDataSource);
|
You can send an existing image file by using any of the three methods such as byte[], javax.activation.DataHandler, or java.awt.Image. The details on the respective procedures are as follows:
The procedure to send an existing image file by using byte[] is as follows:
java.io.FileInputStream fileInputStream =
new java.io.FileInputStream("sample.png");
|
java.io.ByteArrayOutputStream byteArrayOutputStream =
new java.io.ByteArrayOutputStream();
int i = 0;
while ((i = fileInputStream.read()) != -1) {
byteArrayOutputStream.write(i);
}
byte[] bytes = byteArrayOutputStream.toByteArray();
|
The procedure to send an existing image file by using javax.activation.DataHandler is as follows:
javax.activation.FileDataSource fileDataSource =
new javax.activation.FileDataSource("sample.png");
|
javax.activation.DataHandler dataHandler =
new javax.activation.DataHandler(fileDataSource);
|
The procedure to send an existing image file by using java.awt.Image is as follows:
java.awt.Image image =
Toolkit.getDefaultToolkit().createImage("sample.png");
|
You can send an existing XML file by using any of the three methods such as byte[], javax.activation.DataHandler, or javax.xml.transform.Source. The details on the respective procedures are as follows:
The procedure to send an existing XML file by using byte[] is as follows:
java.io.FileInputStream fileInputStream =
new java.io.FileInputStream("sample.xml");
|
java.io.ByteArrayOutputStream byteArrayOutputStream =
new java.io.ByteArrayOutputStream();
int i = 0;
while ((i = fileInputStream.read()) != -1) {
byteArrayOutputStream.write(i);
}
byte[] bytes = byteArrayOutputStream.toByteArray();
|
The procedure to send an existing XML file by using javax.activation.DataHandler is as follows:
javax.activation.FileDataSource fileDataSource =
new javax.activation.FileDataSource("sample.xml");
|
javax.activation.DataHandler dataHandler =
new javax.activation.DataHandler(fileDataSource);
|
The procedure to send an existing XML file by using javax.xml.transform.Source is as follows:
javax.xml.transform.stream.StreamSource streamSource =
new javax.xml.transform.stream.StreamSource("sample.xml");
|
You can send the java.lang.String object by using either of the two methods such as byte[] or javax.activation.DataHandler. The details on the respective procedures are as follows:
The procedure to send the java.lang.String object by using byte[] is as follows:
java.lang.String str =
new java.lang.String("abcde");
|
byte[] bytes =
str.getBytes();
|
The procedure to send the java.lang.String object by using javax.activation.DataHandler is as follows:
java.lang.String str =
new java.lang.String("abcde");
|
javax.activation.DataHandler dataHandler =
new javax.activation.DataHandler(str, "text/plain; charset=UTF-8");
|
When sending a text file, an XML file or a java.lang.String object (String) as a javax.activation.DataHandler object through an attachment in the MTOM/XOP specification format, you can specify the character code of the characters to be included in the object by using the DataHandler (Object, String) constructor of the javax.activation.DataHandler class in the same way as the codes are specified in an attachment in the wsi:swaRef format.
For details on how to specify a character code where the DataHandler (Object, String) constructor was used, see 28.5.1(4) Precautions on generating the javax.activation.DataHandler object.
All Rights Reserved. Copyright (C) 2013, Hitachi, Ltd.