⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileserviceclient.java

📁 精通Jboss——Ejb和Web Services开发精解的随书源代码
💻 JAVA
字号:
package com.liuyang.axis.soapattachment;

import java.net.URL;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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.XMLType;
import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;

public class FileServiceClient {

	public static void main(String[] args) throws Exception {
		Send();
		Receive();
	}
	public static String filename = "./att/file.xml";
	public static void Send() throws Exception{
		DataHandler dhSource = new DataHandler(new FileDataSource(filename));
		System.out.println(dhSource.getName());
		Service service = new Service();
		Call call = (Call) service.createCall();
		String endpoint = "http://localhost:8080/axis/services/FileService";
		call.setTargetEndpointAddress(new URL(endpoint));
		call.setOperationName(new QName("FileService", "putFile")); 
		QName qnameAttachment = new QName("FileService", "DataHandler");
		call.registerTypeMapping(dhSource.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 );
		Object ret = call.invoke(new Object[]{dhSource,"test.txt"}); 
		System.out.println(ret);		
	}
	
	public static void Receive()throws Exception{
		DataHandler dhSource = new DataHandler(new FileDataSource(filename));
		Service service = new Service();
		Call call = (Call) service.createCall();
		String endpoint = "http://localhost:8080/axis/services/FileService";
		call.setTargetEndpointAddress(new URL(endpoint));
		call.setOperationName(new QName("FileService", "getFile")); 
		QName qnameAttachment = new QName("http://localhost:8080/axis/services/FileService", "DataHandler");
		call.registerTypeMapping(dhSource.getClass(),qnameAttachment,
			JAFDataHandlerSerializerFactory.class,JAFDataHandlerDeserializerFactory.class);
		call.addParameter("s",XMLType.XSD_STRING ,ParameterMode.IN);
		call.setReturnType(qnameAttachment);
		Object ret = call.invoke(new Object[]{"test.txt"});
		if(ret instanceof DataHandler){
			DataHandler dadahandler = (DataHandler) ret;
			System.out.println(dadahandler.getContent());
		}
		if(ret ==null){
			System.out.println("result is null");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -