服务端在jdk1.6上,webservice使用的是Axis2。
jdk1.5或更低调用webservice需使用Axis1.*(注:Axis2基于jdk1.6,低版本运行报错)。
客户端:例如
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.RemoteException;
import javax.activation.DataHandler;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import
org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
import
org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;
public class FileReceiveClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
String url =
"http://127.0.0.1:8090/myAxis/services/FileSendServer";
Service service = new Service();
Call call;
DataHandler handler;
InputStream input = null;
FileOutputStream fos = null;
String filepath = "d:/upload/";
String filename = filepath + "tests.txt";
File file = new File(filename);
try {
call = (Call) service.createCall();
QName qn = new QName("ns1:DataHandler", "DataHandler");
call.setTargetEndpointAddress(url);
call.setOperationName(new QName("FileSendServer", "getFile"));
call.registerTypeMapping(DataHandler.class, qn,
JAFDataHandlerSerializerFactory.class,
JAFDataHandlerDeserializerFactory.class);
//
call.addParameter("source", XMLType.XSD_STRING ,ParameterMode.IN);
//设置服务调用方法的传入参数类型
call.setReturnType(qn,DataHandler.class);//需要返回的QName以及返回的类型是DataHandler.class
handler = (DataHandler) call.invoke(new Object[] {});
input = handler.getInputStream();
fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
while (input.read(buffer) != -1) {
fos.write(buffer);
}
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
if (fos != null) {
try {
fos.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
}
加载中,请稍候......