📄 fileclient.java
字号:
package anni.file;
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 FileClient {
Log log = LogFactory.getLog(FileClient.class);
String endPoint = "http://localhost:8080/axis/services/FileService";
QName qNameAttachment = new QName("FileService", "DataHandler");
Call call;
public FileClient() {
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 = "build.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 {
FileClient client = new FileClient();
client.testPutFile();
client.testGetFile();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -