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部署描述文件如下:
DataHandler dh=new DataHandler(new FileDataSource(filename));
String endpoint="http://127.0.0.1:7003/axis/services/FileService";
String name="gaolong1";
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName("http://127.0.0.1:7001/axis/services/FileService", "putFile"));//指定方法的命名空间
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"?>HelloWorld.java
------=_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可以来实现,只是比较底层而已。