使用AXIS实现文件传输(转)

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值