Server端代码:
package org.demo.yuzp.file;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.activation.DataHandler;
public class FileReceiverServer {
public String receive(DataHandler handler, String fileName){
File file = new File(fileName);
if (handler == null || fileName == null || "".equals(fileName)){
return "errors";
}
InputStream input = null;
FileOutputStream fos = null;
try {
input = handler.getInputStream();
fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
while (input.read(buffer) != -1){
fos.write(buffer);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if (input != null){
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return "Success file saved on server, at: " + file.getAbsolutePath();
}
}
server-config.wsdd
<service name="FileReceiverServer" provider="java:RPC"> <parameter name="className" value="org.demo.yuzp.file.FileReceiverServer" /> <parameter name="allowedMethods" value="*" /> <parameter name="scope" value="session" /> <operation name="receive" qname="ns:receive" xmlns:ns="receive" returnType="rtns:string" xmlns:rtns="http://www.w3.org/2001/XMLSchema"> <parameter name="handler" type="tns:string" xmlns:tns="http://www.w3.org/2001/XMLSchema" /> <parameter name="fileName" type="myns:DataHandler" xmlns:tns="http://www.w3.org/2001/XMLSchema" /> </operation> <typeMapping qname="myns:DataHandler" xmlns:myns="DataHandler" languageSpecificType="java:javax.activation.DataHandler" serializer="org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory" deserializer="org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </service>
看了deploy配置文件后,大家也许会问,DataHandler就是用来作文件序列化操作的,那为什么在wsdd文件中还要配置文件序列化/反序列化器呢?
这是因为DataHandler本身是需要序列化和反序列化的,所以必须要用Axis提供的序列化/反序列化器JAFDataHandlerSerializerFactory和JAFDataHandlerDeserializerFactory来进行序列化/反序列化操作。
Client端代码:
package org.demo.yuzp.file;
import java.rmi.RemoteException;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.XMLType;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;
public class FileSenderClient {
/**
* @param args
*/
public static void main(String[] args) {
String fileName = "readme.txt";
DataHandler dh = new DataHandler(new FileDataSource(
FileSenderClient.class.getResource(fileName).getPath()));
System.out.println("PATH:" +
FileSenderClient.class.getResource(fileName).getPath());
String url = "http://localhost:8080/axis1/services/FileReceiverServer";
Service serv = new Service();
try {
Call call = (Call)serv.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName(url, "receive"));
QName qn = new QName("DataHandler", "myns:DataHandler");
call.registerTypeMapping(DataHandler.class, qn, JAFDataHandlerSerializerFactory.class, JAFDataHandlerDeserializerFactory.class);
call.addParameter("s1", qn, ParameterMode.IN);
call.addParameter("s2", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnClass(String.class);
String result = (String)call.invoke(new Object[]{dh, fileName});
System.out.println("result: " + result);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
当运行完Client代码后,会在Console中显示出:
Success file saved on server, at: E:\Eclipse3.4.2\readme.txt
package org.demo.yuzp.file;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.activation.DataHandler;
public class FileReceiverServer {
public String receive(DataHandler handler, String fileName){
File file = new File(fileName);
if (handler == null || fileName == null || "".equals(fileName)){
return "errors";
}
InputStream input = null;
FileOutputStream fos = null;
try {
input = handler.getInputStream();
fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
while (input.read(buffer) != -1){
fos.write(buffer);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if (input != null){
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return "Success file saved on server, at: " + file.getAbsolutePath();
}
}
server-config.wsdd
<service name="FileReceiverServer" provider="java:RPC"> <parameter name="className" value="org.demo.yuzp.file.FileReceiverServer" /> <parameter name="allowedMethods" value="*" /> <parameter name="scope" value="session" /> <operation name="receive" qname="ns:receive" xmlns:ns="receive" returnType="rtns:string" xmlns:rtns="http://www.w3.org/2001/XMLSchema"> <parameter name="handler" type="tns:string" xmlns:tns="http://www.w3.org/2001/XMLSchema" /> <parameter name="fileName" type="myns:DataHandler" xmlns:tns="http://www.w3.org/2001/XMLSchema" /> </operation> <typeMapping qname="myns:DataHandler" xmlns:myns="DataHandler" languageSpecificType="java:javax.activation.DataHandler" serializer="org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory" deserializer="org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </service>
看了deploy配置文件后,大家也许会问,DataHandler就是用来作文件序列化操作的,那为什么在wsdd文件中还要配置文件序列化/反序列化器呢?
这是因为DataHandler本身是需要序列化和反序列化的,所以必须要用Axis提供的序列化/反序列化器JAFDataHandlerSerializerFactory和JAFDataHandlerDeserializerFactory来进行序列化/反序列化操作。
Client端代码:
package org.demo.yuzp.file;
import java.rmi.RemoteException;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.XMLType;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;
public class FileSenderClient {
/**
* @param args
*/
public static void main(String[] args) {
String fileName = "readme.txt";
DataHandler dh = new DataHandler(new FileDataSource(
FileSenderClient.class.getResource(fileName).getPath()));
System.out.println("PATH:" +
FileSenderClient.class.getResource(fileName).getPath());
String url = "http://localhost:8080/axis1/services/FileReceiverServer";
Service serv = new Service();
try {
Call call = (Call)serv.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName(url, "receive"));
QName qn = new QName("DataHandler", "myns:DataHandler");
call.registerTypeMapping(DataHandler.class, qn, JAFDataHandlerSerializerFactory.class, JAFDataHandlerDeserializerFactory.class);
call.addParameter("s1", qn, ParameterMode.IN);
call.addParameter("s2", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnClass(String.class);
String result = (String)call.invoke(new Object[]{dh, fileName});
System.out.println("result: " + result);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
当运行完Client代码后,会在Console中显示出:
Success file saved on server, at: E:\Eclipse3.4.2\readme.txt