目标:调用接口服务器端读取文件传输到客户端。
这里服务器端使用TOMCAT服务器,客户端直接在Eclipse中跑个小的应用程序将接收到的文件保存在硬盘(服务器端需要配置AXIS运行环境,这里不再描述)。
1,FilesObject.java
package cn.ioid.test.axis;
import javax.activation.DataHandler;
/**
* 使用axis1.4实现webservice传输文件,返回结果对象
*/
public class FilesObject {
private String[] fileNames = null;
private DataHandler[] files = null;
public String[] getFileNames() {
return fileNames;
}
public void setFileNames(String[] fileNames) {
this.fileNames = fileNames;
}
public DataHandler[] getFiles() {
return files;
}
public void setFiles(DataHandler[] files) {
this.files = files;
}
}
package cn.ioid.test.axis;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
/**
* 使用axis1.4实现webservice传输文件,接口服务端
*
这里传入一个int型简单参数,返回一个自定义对象
*/
public class FilesGetter {
public FilesObject getFiles(int inparam){
FilesObject fo = new FilesObject();
fo.setFileNames(new String[]{"test.xls"});
FileDataSource fds = new FileDataSource("D:\\temp\\test.xls");
fo.setFiles(new DataHandler[]{new DataHandler(fds)});
return fo;
}
}
3,配置服务端的server-config.wsdd,添加:
java -Djava.ext.dirs=D:\Tomcat5.5.20\webapps\axis\WEB-INF\lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/services/testAxis?wsdl -p cn.ioid.test.axis
注意-p参数,不写的话axis自动生成包名称比较乱。
package cn.ioid.test.axis;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import javax.activation.DataHandler;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
public class TestMain {
public static void main(String[] args) {
Service service = new Service();
try {
Call call = (Call)service.createCall();
URL u = new URL("http://localhost:8080/axis/services/testAxis?wsdl");
call.setTargetEndpointAddress(u);
call.setOperationName(new QName("urn:FilesGetter","getFiles"));
call.addParameter("lastId",org.apache.axis.Constants.XSD_STRING,ParameterMode.IN);
QName qnb = new QName("urn:BeanService","FilesObject");
call.registerTypeMapping(FilesObject.class,qnb,new BeanSerializerFactory(FilesObject.class,qnb)
,new BeanDeserializerFactory(FilesObject.class,qnb));
call.setReturnType(qnb);
Object rtObj = call.invoke("getFiles",new Object[]{new Integer("0")});
if (rtObj != null) {
FilesObject fo = (FilesObject)rtObj;
String[] pns = fo.getFileNames();
DataHandler[] dhs = fo.getFiles();
for(int i=0; i
结束