Thursday, August 27, 2009

Webservice Client for sending attachments using apace axis

Today i am going to share for my experience how to send binary attachments to a webservice using apache axis.The following piece of code will help you to do the same.

String endpoint = "http://localhost:9000/ecm/yourwebservice.jws";
javax.activation.DataHandler dataHandler = new javax.activation.DataHandler(
new FileDataSource(
"C:\\WINDOWS\\TEMP\\user\\data\\metadata.xml"));
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
QName qnameAttachment = new QName("uploadUser", "DataHandler");
call.addParameter("source", qnameAttachment, ParameterMode.IN);
call.registerTypeMapping(dataHandler.getClass(), // Add serializer for attachment.
qnameAttachment, JAFDataHandlerSerializerFactory.class,
JAFDataHandlerDeserializerFactory.class);
call.setOperationName("uploadUser");
call.invoke(new Object[] { dataHandler});

The above code is the client.


The webservice will be something like this

public void uploadUser(DataHandler handler){
InputStream in = handler.getInputStream();
//in is the bytes send by the client to the webservice

}

Thats all how we can send attachments to webservice.

Happy Coding