Java Reference
In-Depth Information
Passing Binary Data in a Request
Problem
You need to send binary data, such as an image file or PDF, within the body of a SOAP mes-
sage.
Solution
Use a byte array to represent your binary data. The WSDL will represent this as xs:base64
or xs:hexBinary .
Example 6-16 demonstrates this. The use case idea here is a web service operation called
getImage , which accepts a string as input. The string is an identifier for an image in a database
that has been previously stored as a BLOB (Binary Large Object). You could use the ID to
perform a SQL query and get the BLOB out, returning it as a byte array. To keep it simple and
focused, though, just create a regular byte array and return it.
Example6-16.Binary data method
@WebMethod
public @WebResult(name="imageResponse",
targetNamespace="http://ns.soacookbook.com/ch03")
byte[]
getImage(
@WebParam(name="imageRequest",
targetNamespace="http://ns.soacookbook.com/ch03")
String imageId) {
//Use the passed ID to find this instance in the database
//This is our fake image data...
byte[] imageBytes = {1,0};
//If you want to save your image data to a database,
//create a PreparedStatement and use:
...
ps.setBinaryStream(1,
new ByteArrayInputStream(sigImageData), imageBytes.length);
return imageBytes;
}
Search WWH ::




Custom Search