使用Axis傳送附件有兩種方式:
1. 將你要傳送的文件封裝在DataHandler中,然后將DataHandler對象或DataHandler數(shù)組(多個(gè)文件傳送的時(shí)候)作為客戶端調(diào)用函數(shù)的參數(shù)(從客戶端上傳文件到服務(wù)器)Axis服務(wù)的返回類型(從服務(wù)器端下載文件到客戶端)進(jìn)行傳輸。
2. 還有一種方式是直接修改soap信封的內(nèi)容,將附件信息加到soap信封中發(fā)送。
這里我們只討論第一種方法,因?yàn)樗鼘?shí)現(xiàn)起來非常簡單。關(guān)于第二種方法在Axis包的webapps/attachments/TestRf.java中有詳細(xì)的原碼可以參考。
下面的例子是把文件從服務(wù)器端下載到客戶端:
1.服務(wù)端程序:
假設(shè)傳輸多個(gè)文件:在服務(wù)器端將文件取出來,并將文件封裝在DataHandler數(shù)組中。
代碼如下:
DataHandler[] ret = new DataHandler[totalFileNum];
... ...
java.io.File myFile = new java.io.File(filePath);
if(myFile.isFile() && myFile.canRead())
{
String fname = myFile.getAbsoluteFile().getCanonicalPath();
DataHandler[0] = new DataHandler(new FileDataSource(fname));
}
... ...
return ret;
上面的代碼將所有的文件封裝在了DataHandler數(shù)組中,并返回。
2. 客戶端的訪問:
代碼如下:
Service service = new Service();
Call call = (Call) service.createCall();
URL myURL = new URL(" call.setTargetEndpointAddress(myURL); //設(shè)定服務(wù)的主機(jī)和位置
call.setOperationName(new QName("urn:MyAttachServer","echoDir")); //設(shè)置要調(diào)用的服務(wù)的方法
QName qnameAttachment = new QName("urn:MyAttachServer","DataHandler");
call.registerTypeMapping(DataHandler.class, qnameAttachment, JAFDataHandlerSerializerFactory.class,JAFDataHandlerDeserializerFactory.class); //為附件(即DataHandler類)創(chuàng)建序列化生成器
call.addParameter("source", XMLType.XSD_STRING ,ParameterMode.IN); //設(shè)置服務(wù)調(diào)用方法的傳入?yún)?shù)類型
call.setReturnType(XMLType.SOAP_ARRAY); //設(shè)置調(diào)用服務(wù)方法的返回類型,由于返回的是DataHandler數(shù)組,所以設(shè)置為SOAP_ARRAY類型
javax.activation.DataHandler[] ret = (javax.activation.DataHandler[])call.invoke(new Object[]{null}); //調(diào)用方法
for (i = 0; i < ret.length; ++i)
{
DataHandler recDH = ret[i];
java.io.File receivedFile = new java.io.File(recDH.getName()); //文件生成
}
3. 服務(wù)的部署:
注意:你要在部署的時(shí)候,定義DataHandler的序列化生成器。
編寫deploy.wsdd文件:
<deployment xmlns=" <service name="urn:att_STC_Server" provider="java:RPC" >
<parameter name="className" value="samples.att_STC.att_STC_Server"/>
<parameter name="allowedMethods" value="echoDir"/>
<typeMapping deserializer="org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory"
languageSpecificType="java:javax.activation.DataHandler" qname="ns1:DataHandler"
serializer="org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</service>
</deployment>
運(yùn)行java org.apache.axis.client.AdminClient %* deploy.wsdd,部署服務(wù)。
posted on 2006-02-20 16:28
小鐵匠 閱讀(1035)
評論(1) 編輯 收藏 所屬分類:
java