1.為了使其對DataHandler的支持。除了配置好axis環境之外,還要在sun的網站上下載jaf 1-0-2.jar包,并把它注冊到CLASS_PATH中。
2.編寫服務程序(.java),既支持文本文件,也支持二進制文件。
package test.gaolong;
import java.io.*;
import javax.activation.*;
public class FileService{
public static String Repository="./files/";
public String putFile(DataHandler dh,String name){
if(name==null)
name="test.tmp";
System.out.println("test");
try{
File dir=new File(Repository);
if(!dir.exists()){
dir.mkdir(); System.out.println("makedir"+"test");
}
InputStream input=dh.getInputStream();
FileOutputStream fos=new FileOutputStream(new File(dir,name));
System.out.println("test");
byte[] buffer=new byte[1024*4];
int n=0;
while((n=input.read(buffer))!=-1){
fos.write(buffer,0,n);
System.out.println(buffer);
}
System.out.println("test");
input.close();
fos.close();
}catch(IOException e){
e.printStackTrace();
}
return name+"send OK";
}
public DataHandler getFile(String name){
File dir=new File(Repository);
if(!dir.exists())
dir.mkdir();
File data=new File(dir,name);
if(data.exists())
return new DataHandler(new FileDataSource(data));
else
return null;
}
}
3。寫deploy.wsdd部署描述文件如下:
<deployment xmlns="http://localhost:7001/axis/services/FileService" >
<service name="FileService" provider="java:RPC">
<parameter name="className" value="test.gaolong.FileService"/>
<parameter name="allowedMethods" value="*"/>
<parameter name="allowedRoles" value="user"/>
<operation name="getFile" returnQName="returnqname" returnType="ns1:DataHandler" xmlns:SchemaNS=" <parameter name="name" type="SchemaNS:string"/>
</operation>
<operation name="putFile" returnQName="returnqname" returnType="ns1:DataHandler" xmlns:SchemaNS=" <parameter name="dh" type="ns1:DataHandler"/>
<parameter name="name" type="SchemaNS:string"/>
</operation>
<typeMapping deserializer="org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory" type="java:javax.activation.DataHandler" qname="ns1:DataHandler" serializer="org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/%22/%3E//注意見下面的。
</service>
</deployment>
注:對于soap1.2而言,要使用上面的<typeMaping>,而對于soap1.1而言,則有一點區別如下:
<typeMapping deserializer="org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory" lanuageSpecificType="java:javax.activation.DataHandler" qname="ns1:DataHandler" serializer="org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/%22/>
4。啟動服務器,用java org.apache.axis.client.AdminClient -p 7001 deploy.wsdd部署webservices即可訪問。
5。編寫客戶端應用訪問程序如下:
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.namespace.QName;
import org.apache.axis.soap.SOAP11Constants;
import java.net.URL;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.axis.encoding.ser.*;
public class ServiceClient{
public static void main(String[] args) throws Exception{
String filename="HelloWorld.java";
DataHandler dh=new DataHandler(new FileDataSource(filename));
String endpoint=" String name="gaolong1";
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName(" QName qnameattachment=new QName("FileService","DataHandler");
call.registerTypeMapping(dh.getClass(),qnameattachment,JAFDataHandlerSerializerFactory.class,JAFDataHandlerDeserializerFactory.class);
call.addParameter("s1",qnameattachment,ParameterMode.IN);
call.addParameter("s2",XMLType.XSD_STRING,ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);//XMLType.XSD_STRING);//用Class.forName("java.lang.String")來獲取java類型
String ret=(String)call.invoke(new Object[] {dh,"HelloWorld.java"});
System.out.println(ret);
}
}
6。成功執行客戶端應用程序,可以找到上傳文件。
請求的soap消息:
POST /axis/services/FileService HTTP/1.0
Content-Type: multipart/related; type="text/xml"; start="<3165C8664597DC7EF29D5BFAC8972562>"; boundary="----=_Part_0_21866740.1141202759484"
Accept: application/soap+xml, application/dime, multipart/related, text/*
User-Agent: Axis/1.2.1
Host: localhost:7003
Cache-Control: no-cache
Pragma: no-cache
SOAPAction: ""
Content-Length: 1050
------=_Part_0_21866740.1141202759484
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: binary
Content-Id: <3165C8664597DC7EF29D5BFAC8972562>
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2001/XMLSchema-instance%22%3E%3Csoapenv:Body%3E%3Cns1:putFile soapenv:encodingStyle="------=_Part_0_21866740.1141202759484
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-Id: <03D9C1D3A9E0788D274934C3ABD52811>
public class HelloWorld{
public String sayHello(String name){
return "Hello"+name;
}
}
------=_Part_0_21866740.1141202759484--
上傳后的文件的位置:/*在該目錄下創建文件夾:D:\bea\user_projects\domains\mydomain\files,并把相應的文件存入該目錄下*/
7.另一種基于java mail的帶附件的傳輸,是基于xmlDOM+servlet可以來實現,只是比較底層而已。