Upload & Download File from Axis server
1. server part:
FileService.java
package cn.com.mytest.server;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class FileService {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
}
Log log = LogFactory.getLog(FileService.class);
public static String Repository = "./files/";
public String putFile(DataHandler dh, String name) {
if (name == null)
name = "test.tmp";
log.info("name : " + name);
try {
File dir = new File(Repository);
if (!dir.exists()) {
dir.mkdir();
log.info("makedir : dir : " + dir);
}
InputStream input = dh.getInputStream();
FileOutputStream fos = new FileOutputStream(new File(dir, name));
byte[] buffer = new byte[1024 * 4];
int n = 0;
while ((n = input.read(buffer)) != -1) {
fos.write(buffer, 0, n);
//log.info(buffer);
}
input.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return name + "send OK";
}
public DataHandler[] getFile(String name) {
log.info("name : " + name);
File dir = new File(Repository);
if (!dir.exists())
dir.mkdir();
if(name != null && !name.equals("")) {
File data = new File(dir, name);
if (data.exists()) {
DataHandler[] handlers = new DataHandler[1];
handlers[0] = new DataHandler(new FileDataSource(data));
log.info("handlers : " + handlers + " [" + handlers.length + "]");
return handlers;
} else {
log.info("return : null");
return null;
}
} else {
File[] files = dir.listFiles();
DataHandler[] handlers = new DataHandler[files.length];
for(int i=0; i<files.length; i++) {
handlers[i] = new DataHandler(new FileDataSource(files[i]));
}
log.info("handlers : " + handlers + " [" + handlers.length + "]");
return handlers;
}
}
}
deploy.wsdd
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="FileService" provider="java:RPC">
<parameter name="className" value="cn.com.mytest.server.FileService"/>
<parameter name="allowedMethods" value="*"/>
<operation name="getFile" returnQName="returnqname" returnType="ns1:DataHandler" xmlns:SchemaNS="http://www.w3.org/2001/XMlSchema">
<parameter name="name" type="SchemaNS:string"/>
</operation>
<operation name="putFile" returnQName="returnqname" returnType="ns1:DataHandler" xmlns:SchemaNS="http://www.w3.org/2001/XMlSchema">
<parameter name="dh" type="ns1:DataHandler"/>
<parameter name="name" type="SchemaNS:string"/>
</operation>
<typeMapping deserializer="org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory"
type="java:javax.activation.DataHandler" qname="ns1:DataHandler"
serializer="org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</service>
</deployment>
2. client part:
package cn.com.mytest.client;
import java.io.File;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.net.URL;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.axis.attachments.AttachmentPart;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.*;
import org.apache.axis.soap.SOAP11Constants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class TestClientFileService {
Log log = LogFactory.getLog(TestClientFileService.class);
String endPoint = "http://localhost:8080/axis/services/FileService";
QName qNameAttachment = new QName("FileService", "DataHandler");
Call call;
public TestClientFileService() {
init();
}
void init() {
try {
Service service = new Service();
call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(endPoint));
call.registerTypeMapping(DataHandler.class, qNameAttachment,
JAFDataHandlerSerializerFactory.class,
JAFDataHandlerDeserializerFactory.class);
} catch (Exception ex) {
log.fatal(ex);
System.exit(1);
}
}
void testPutFile() throws Exception {
log.info("start");
init();
String fileName = "fj.xml";
DataHandler dataHandler = new DataHandler(new FileDataSource(fileName));
call.setOperationName(new QName(endPoint, "putFile"));// 指定方法的命名空间
call.addParameter("s1", qNameAttachment, ParameterMode.IN);
call.addParameter("s2", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);//用Class.forName("java.lang.String")来获取java类型
String result = (String) call.invoke(new Object[] { dataHandler, fileName });
log.info(result);
}
void testGetFile() throws Exception {
log.info("start");
init();
try {
call.setOperationName(new QName(endPoint, "getFile"));
call.addParameter("name", XMLType.XSD_STRING, ParameterMode.IN);//设置服务调用方法的传入参数类型
call.setReturnType(XMLType.SOAP_ARRAY);//设置调用服务方法的返回类型,由于返回的是DataHandler数组,所以设置为SOAP_ARRAY类型
Object[] result = (Object[])call.invoke(new Object[]{""});
for(int i=0; i<result.length; i++) {
log.info("test");
AttachmentPart part = (AttachmentPart)result[i];
File receiveFile = new File(part.getDataHandler().getName());
log.info(receiveFile);
}
} catch (Exception ex) {
log.error(ex);
}
log.info("end");
}
public static void main(String[] args) throws Exception {
TestClientFileService client = new TestClientFileService();
//client.testPutFile();
client.testGetFile();
}
}
** file.xml: d:/mydemos/axis_text/fj.xml
** upload to axis server: %tomcat_home%/bin/files/......
** download files types: .att (can open: txt,html,doc,xls etc)