public static void main(String args[]){ try{ String filename = "D:\\images\\products\\r.jpg"; //create the data for the attached file DataHandler dhSource = new DataHandler(new FileDataSource(filename)); String endpointURL = "http://localhost:8080/axis/services/AttachmentService"; String methodName = "addImage";
Service service = new Service(); Call call = (Call)service.createCall(); call.setTargetEndpointAddress(new URL(endpointURL)); call.setOperationName(new QName("AttachmentService",methodName)); call.addParameter("sku",XMLType.XSD_STRING,ParameterMode.PARAM_MODE_IN); QName qname = new QName("AttachmentService","DataHandler"); call.addParameter("image",qname,ParameterMode.PARAM_MODE_IN); //register the datahandler call.registerTypeMapping(dhSource.getClass(),qname,JAFDataHandlerSerializerFactory.class,JAFDataHandlerDeserializerFactory.class); call.setReturnType(XMLType.XSD_STRING);
Object[] params = new Object[]{"SKU-111",dhSource}; String result = (String)call.invoke(params);
public String addImage(String sku,DataHandler dataHandler){ System.out.println("trying"); try{ String filepath = "c:/wrox-axis/"+sku+"-image.jpg"; FileOutputStream fout = new FileOutputStream(new File(filepath)); BufferedInputStream in = new BufferedInputStream(dataHandler.getInputStream()); while(in.available()!=0){ fout.write(in.read()); } }catch(Exception e){ return e.toString(); } return "Image: "+sku+" has been added successfully!!"; }
}
I did a test by stripping out the attachment being sent by the client and just let it send the string, then in the webservice I stripped out the lines for the attachment and just returned the string and it worked ok, so it has been deployed correctly.
I have the Java Activation framework both in tomcat commons and my webapps lib dir.
I'm pretty sure the error is being thrown here, public String addImage(String sku,DataHandler dataHandler){
any help would be greatly appreciated, thank you, JP.
Balaji Loganathan
author and deputy
Bartender
Joined: Jul 13, 2001
Posts: 3150
posted
0
Welcome to Javaranch John. Can you post the full error starting with "Null pointer exception" Also did you traced the SOAP message using soapmonitor or TCPIP monitor. Also double check the WSDD file and check whether the you have defined serializer for file attachments.
C:\wrox-axis>java chapter5.AttachmentServiceClient java.lang.NullPointerException at org.apache.axis.AxisFault.makeFault(Unknown Source) at org.apache.axis.SOAPPart.getAsString(Unknown Source) at org.apache.axis.SOAPPart.getAsBytes(Unknown Source) at org.apache.axis.Message.getContentLength(Unknown Source) at org.apache.axis.transport.http.HTTPSender.invoke(Unknown Source) at org.apache.axis.strategies.InvocationStrategy.visit(Unknown Source) at org.apache.axis.SimpleChain.doVisiting(Unknown Source) at org.apache.axis.SimpleChain.invoke(Unknown Source) at org.apache.axis.client.AxisClient.invoke(Unknown Source) at org.apache.axis.client.Call.invoke(Unknown Source) at org.apache.axis.client.Call.invoke(Unknown Source) at org.apache.axis.client.Call.invoke(Unknown Source) at org.apache.axis.client.Call.invoke(Unknown Source) at chapter5.AttachmentServiceClient.main(AttachmentServiceClient.java:45 ) Caused by: java.lang.NullPointerException at org.apache.axis.encoding.ser.JAFDataHandlerSerializer.serialize(Unkno wn Source) at org.apache.axis.encoding.SerializationContextImpl.serializeActual(Unk nown Source) at org.apache.axis.encoding.SerializationContextImpl.serialize(Unknown S ource) at org.apache.axis.encoding.SerializationContextImpl.outputMultiRefs(Unk nown Source) at org.apache.axis.message.SOAPEnvelope.outputImpl(Unknown Source) at org.apache.axis.message.MessageElement.output(Unknown Source) ... 13 more
java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:168) at org.apache.axis.utils.tcpmon$Connection.run(Unknown Source)
I also tested tcp on another webservice and it worked correctly,