axis2和JDK1.5开发(文件传输服务)详解一(图解)

1.准备资源:axis2涉及到的jar包和发布环境war包,地址如下:
http://apache.mirror.phpchina.com/ws/axis2/1_4_1/axis2-1.4.1-bin.zip
http://apache.mirror.phpchina.com/ws/axis2/1_4_1/axis2-1.4.1-war.zip
2.将下载后的war包解压成axis2.war,拷贝到tomcat的webapps下,启动tomcat,访问地址http://localhost:8081/axis2,如果看到那只似曾相识apache页面,那么恭喜你,成功了!
[img]http://dl.iteye.com/upload/attachment/270833/1d70b814-0cac-3494-bb44-9a7e3beeea31.jpg[/img]


2.接着安装Eclipse插件,插件有两个,客户端代码插件axis2-eclipse-codegen-wizard.zip和服务端代码生成插件axis2-eclipse-service-archiver-wizard.zip,一般开发服务端的人只要有服务端代码生成插件即可,如果我们使用别人发布的WS,那么会用到客户端代码生成插件。总之都装上就是了,用的时候就方便了,将两个插件直接拷贝到myEclipse的 HOME\eclipse\plugins\下,然后再MyEclipse中选择File--new--others,打开new窗口。如果窗口中有Axis2 Wizards这个选项,那么插件安装成功!

[img]http://dl.iteye.com/upload/attachment/270815/04bbdda4-da6f-3f8b-9dd3-8d3e7307feb6.jpg[/img]

3.编写服务端代码,新建一个web工程,记得将下载的axis2-1.4.1-bin.zip中的lib\*.jar都关联到工程的library中,下面是一段负责上传下载文件等功能的webservice服务端代码,仅供参考:


package com.hollyinfo.cuibao.webservice;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axis2.AxisFault;

import com.hollyinfo.cuibao.logger.HollyinfoLog;
import com.hollyinfo.cuibao.logger.HollyinfoLogger;
import com.hollyinfo.cuibao.util.Config;

public class FileTransferServer {
public static final String UPLOAD_PATH = Config.getInstance().getProperty("UPLOAD_PATH");
public static final String DOWNLOAD_PATH = Config.getInstance().getProperty("DOWNLOAD_PATH");
private static final HollyinfoLog LOG = HollyinfoLogger.getLog(FileTransferServer.class);
public OMElement upload(OMElement element) throws Exception {
LOG.info("正在接收文件!");
FileOutputStream ops=null;
InputStream ips=null;
OMElement ele=null;
try{
OMElement _fileContent = null;//文件内容
OMElement _fileName = null;//文件名
OMElement _fileType = null;//文件类型
for (Iterator _iterator = element.getChildElements(); _iterator
.hasNext();) {
OMElement _ele = (OMElement) _iterator.next();
if (_ele.getLocalName().equalsIgnoreCase("fileContent")) {
_fileContent = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileName")) {
_fileName = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
_fileType = _ele;
}
}

if (_fileContent == null || _fileType == null) {
throw new AxisFault("Either Image or FileName is null");
}

OMText binaryNode = (OMText) _fileContent.getFirstOMChild();
String fileName = _fileName.getText();
String fileType = _fileType.getText();
String storeDir = UPLOAD_PATH ;
File dir = new File(storeDir);
if (!dir.exists()) {
dir.mkdir();
}
String filePath = storeDir + fileName + "." + fileType;
File uploadFile = new File(filePath);
// 解析上传数据,通过输出流保存文件
DataHandler actualDH = (DataHandler) binaryNode.getDataHandler();
ips = actualDH.getInputStream();
ops = new FileOutputStream(uploadFile);
//每次读取10KB,写到输出流
int bytesRead=0;
byte[] buf = new byte[10*1024]; //10KB
while((bytesRead = ips.read(buf)) != -1){
ops.write(buf, 0, bytesRead);
}
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace("http://example.org/filedata", "fd");
ele = fac.createOMElement("response", ns);
//检查文件是否完整传输
if(readLastLine(uploadFile,Config.getInstance().getProperty("FILE_CHARSET")).indexOf("EOF")!=-1){
ele.setText("true");
LOG.info("接收文件:"+filePath+" 成功!");
}else{
ele.setText("false");
LOG.info("接收文件:"+filePath+" 没有完整接收!");
}
}
catch(Exception e) {
e.printStackTrace();
LOG.error("上传文件失败",e);
}finally{
try {
if(ips!=null){
ips.close();
}
if(ops!=null){
ops.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}
return ele;
}


/**
* 返回给客户端文件列表
* @param element
* @return
* @throws Exception
*/
public OMElement queryFileList(OMElement element) throws Exception {
OMElement flag = null;//请求标志
//OMElement firstElement=element.getFirstElement();
for (Iterator _iterator = element.getChildElements(); _iterator.hasNext();) {
OMElement _ele = (OMElement) _iterator.next();
if (_ele.getLocalName().equalsIgnoreCase("flag")) {
flag = _ele;
break;
}
}
//如果客户端请求的是查询文件列表则返回文件列表
if("true".equals(flag.getText())){
// String xmlInfo="111.txt";
OMFactory fac = OMAbstractFactory.getOMFactory();
//这个是命名空间,实际情况需要看对方给你的wsdl这个值是什么.这是服务器端随便你定义.客户端需要照这个样子传才行。
//后面那个参数可为空也可以有值。这个东西也好理解调试的时候打印下OMElement 就知道这东西怎么用了。
OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata","fd");
OMElement filelist = fac.createOMElement("queryFileList", omNs);
File dir=new File(DOWNLOAD_PATH);
if(!dir.exists()){
dir.mkdirs();
}
LOG.info("查询待下载的文件列表: " + DOWNLOAD_PATH);
String [] filenames=dir.list();
OMElement _file=null;
for (int i = 0; i 0) {
pos--;
raf.seek(pos);
if (raf.readByte() == '\n') {
break;
}
}
if (pos == 0) {
raf.seek(0);
}
byte[] bytes = new byte[(int) (len - pos)];
raf.read(bytes);
if (charset == null) {
return new String(bytes);
} else {
return new String(bytes, charset);
}
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
} finally {
if (raf != null) {
try {
raf.close();
} catch (Exception e2) {
}
}
}
return null;
}


}


4.编写services.xml文件,这个文件也可以自动生成,这里简单介绍一下这个文件的功能:这个文件用于描述我们将来服务的核心类,已经涉及到其中哪些方法,将来我们使用插件生成*.aar包的过程需要制定这个services.xml文件,最后我们需要部署aar包到tomcat中,这样服务也算发布完成!
(1)、 中name属性的值是服务的名称;
(2)、 值是服务类的全路径;
(3)、 服务类的每个方法都写一个operation标签:


<?xml version="1.0" encoding="UTF-8"?>
<service name="FileTransServer">
<description>
负责发催报信息的webservice
</description>
<parameter name="ServiceClass" locked="false">com.hollyinfo.cuibao.webservice.FileTransferServer</parameter>
<operation name="upload">
<actionMapping>urn:upload</actionMapping>
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
<operation name="download">
<actionMapping>urn:download</actionMapping>
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
<operation name="queryFileList">
<actionMapping>urn:queryFileList</actionMapping>
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
<operation name="deleteFile">
<actionMapping>urn:deleteFile</actionMapping>
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
</service>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值