使用Axis2传输附件(AXIS2 MTOM)--1

本文介绍如何使用Axis2实现文件的上传和下载功能。通过具体的Java代码示例,展示了客户端和服务端的具体实现细节。

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。 http://zhangjunhd.51cto.com/113473/26960
本文介绍如何使用Axis2传递附件。<o:p></o:p>
author: ZJ <st1:chsdate w:st="on" isrocdate="False" year="2007" day="7" islunardate="False" month="5">07-5-7</st1:chsdate>
<o:p> </o:p>
1.工作环境
IDE: Eclipse <st1:chsdate w:st="on" isrocdate="False" year="1899" day="30" islunardate="False" month="12">3.1.2</st1:chsdate>
jdk: jdk<st1:chsdate w:st="on" isrocdate="False" year="1899" day="30" islunardate="False" month="12">1.5.0</st1:chsdate>_04
Tomcat: apache-tomcat-<st1:chsdate w:st="on" isrocdate="False" year="1899" day="30" islunardate="False" month="12">5.0.28</st1:chsdate>
AXIS2:1.0(war版本和bin版本)
<o:p> </o:p>
2.实现<o:p></o:p>
   Eclipse新建一个动态web工程,在WEB-INF\lib下加入axis2所需的jar包。
本例的是一个系统的用户上传下载图片格式文件的例子,每次上传出携带附件外,还包括文件名, 文件类型。此webservice实现的2个功能就是upload, download.
   AXIS2webservice发布的时候是打包成xxx.aar发布的,xxx.aar展开后的目录结构为
 --
    --META-INF
       services.xml
    --包含server端实现的class( 目录跟package是一样的结构)
<o:p> </o:p>
3.服务器端FileTransferServer.java<o:p></o:p>
package sample;<o:p></o:p>
<o:p> </o:p>
import org.apache.axiom.attachments.utils.IOUtils;<o:p></o:p>
import org.apache.axiom.om.OMAbstractFactory;<o:p></o:p>
import org.apache.axiom.om.OMElement;<o:p></o:p>
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;<o:p></o:p>
import org.apache.axiom.om.OMText;<o:p></o:p>
import org.apache.axis2.AxisFault;<o:p></o:p>
<o:p> </o:p>
import java.io.File;<o:p></o:p>
import java.io.FileOutputStream;
import java.io.InputStream;
<o:p> </o:p>
import java.util.Iterator;
<o:p> </o:p>
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
<o:p> </o:p>
public class FileTransferServer {
       public static final String TMP_PATH = "D:/temp";
<o:p> </o:p>
       public OMElement upload(OMElement element) throws Exception {
              OMElement _fileContent = null;//文件内容
              OMElement _fileName = null;//文件名
              OMElement _fileType = null;//文件类型
              System.out.println("The element for upload: " + element);
              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;
                     }
              }
<o:p> </o:p>
              if (_fileContent == null || _fileType == null) {
                     throw new AxisFault("Either Image or FileName is null");
              }
<o:p> </o:p>
              OMText binaryNode = (OMText) _fileContent.getFirstOMChild();
              String fileName = _fileName.getText();
              String fileType = _fileType.getText();
              String storeDir = TMP_PATH + "/" + "tempTest";
              File dir = new File(storeDir);
              if (!dir.exists()) {
                     dir.mkdir();
              }
              String filePath = storeDir + "/" + fileName + "." + fileType;
              File uploadFile = new File(filePath);
              if (uploadFile.exists()) {
                     filePath = storeDir + "/" + fileName + "(1)" + "." + fileType;
                     uploadFile = new File(filePath);
              }
<o:p> </o:p>
              // Extracting the data and saving
              DataHandler actualDH;
              actualDH = (DataHandler) binaryNode.getDataHandler();
<o:p> </o:p>
              FileOutputStream imageOutStream = new FileOutputStream(uploadFile);
              InputStream is = actualDH.getInputStream();
              imageOutStream.write(IOUtils.getStreamAsByteArray(is));
              // setting response
              OMFactory fac = OMAbstractFactory.getOMFactory();
              OMNamespace ns = fac.createOMNamespace("http://example.org/filedata",<o:p></o:p>
                            "fd");
              OMElement ele = fac.createOMElement("response", ns);
              ele.setText("true");
              return ele;
       }
<o:p> </o:p>
       public OMElement download(OMElement element) throws Exception {
              System.out.println("The element for download: " + element);
              OMElement _userName = null;
              OMElement _fileName = null;
              OMElement _fileType = null;
              for (Iterator _iterator = element.getChildElements(); _iterator
                            .hasNext();) {
                     OMElement _ele = (OMElement) _iterator.next();
                     if (_ele.getLocalName().equalsIgnoreCase("userName")) {
                            _userName = _ele;
                     }
                     if (_ele.getLocalName().equalsIgnoreCase("fileName")) {
                            _fileName = _ele;
                     }
                     if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
                            _fileType = _ele;
                     }
              }
              String userName = _userName.getText();
              String fileName = _fileName.getText();
              String fileType = _fileType.getText();
              String filePath = TMP_PATH + "/" + userName + "/" + fileName + "."
                            + fileType;
              System.out.println("The filePath for download: " + filePath);
              FileDataSource dataSource = new FileDataSource(filePath);
              DataHandler expectedDH = new DataHandler(dataSource);
              OMFactory fac = OMAbstractFactory.getOMFactory();
              OMNamespace ns = fac.createOMNamespace("http://example.org/filedata",
                            "fd");
              OMText textData = fac.createOMText(expectedDH, true);
              OMElement ele = fac.createOMElement("response", ns);
              ele.addChild(textData);
              return ele;
       }
}
<o:p> </o:p>
4.services.xml<o:p></o:p>
<service name="FileOperation"></service>
    <description></description>
        This is a sample Web Service with two operations,echo and ping.
   
    <parameter name="ServiceClass" locked="false"></parameter> sample.FileTransferServer
    <operation name="upload"></operation>
        <actionmapping></actionmapping>urn:upload
        <messagereceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"></messagereceiver>
   
      <operation name="download"></operation>
        <actionmapping></actionmapping>urn:download
        <messagereceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"></messagereceiver>
   
<o:p> </o:p>
将这两个文件打包并部署到Tomcat上(略)。
<o:p> </o:p>
5.测试<o:p></o:p>
FileTransferClient.java<o:p></o:p>
package sample;
<o:p> </o:p>
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
<o:p> </o:p>
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
<o:p> </o:p>
import org.apache.axiom.attachments.utils.IOUtils;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;<o:p></o:p>
import org.apache.axiom.om.OMText;<o:p></o:p>
import org.apache.axiom.soap.SOAP11Constants;<o:p></o:p>
import org.apache.axis2.AxisFault;<o:p></o:p>
import org.apache.axis2.Constants;<o:p></o:p>
import org.apache.axis2.addressing.EndpointReference;<o:p></o:p>
import org.apache.axis2.client.Options;<o:p></o:p>
import org.apache.axis2.client.ServiceClient;<o:p></o:p>
 <o:p></o:p>
public class FileTransferClient {
   private static EndpointReference targetEPR =
 new EndpointReference("http://127.0.0.1:8080/axis2/services/FileOperation");
  
   public static boolean upload(String fileName, File file, String fileType) {
     try {
      OMElement data = buildUploadEnvelope(fileName, file, fileType);
      Options options = buildOptions();
      ServiceClient sender = new ServiceClient();
      sender.setOptions(options);
      System.out.println("The data in method upload: "+data);
      OMElement ome = sender.sendReceive(data);
      System.out.println("Convert the data to element in method upload: "+ome);
      String b = ome.getText();
      return Boolean.parseBoolean(b);
     }
     catch(Exception e) {
       e.printStackTrace();
     }
     return false;
   }
  
   public static boolean download(String userName, String fileName, String fileType) {
     try {
       OMElement data = buildDownloadEnvelope(userName, fileName, fileType);
       Options options = buildOptions();
       ServiceClient sender = new ServiceClient();
       sender.setOptions(options);
       System.out.println("The data in method download: "+data);
       OMElement ome = sender.sendReceive(data);
       System.out.println("Convert the data to element in method download: "+ome);
       OMText binaryNode = (OMText) ome.getFirstOMChild();
       binaryNode.setOptimize(true); //必须加此句,否则会出现ContentID is null的异常!
       DataHandler actualDH = (DataHandler) binaryNode.getDataHandler();
       FileOutputStream imageOutStream = new FileOutputStream("D:/userTemp/xx.gif");
       InputStream is = actualDH.getInputStream();
       imageOutStream.write(IOUtils.getStreamAsByteArray(is));
       return true;
      }
      catch(Exception e) {
        e.printStackTrace();
      }
     return false;
   }
  
   private static OMElement buildUploadEnvelope(String fileName, File file, String fileType) {
     DataHandler expectedDH;
     OMFactory fac = OMAbstractFactory.getOMFactory();
     OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata", "fd");<o:p></o:p>
     OMElement data = fac.createOMElement("upload", omNs);
     OMElement fileContent = fac.createOMElement("fileContent", omNs);
     FileDataSource dataSource = new FileDataSource(file);
     expectedDH = new DataHandler(dataSource);
     OMText textData = fac.createOMText(expectedDH, true);
     fileContent.addChild(textData);
     OMElement _fileName = fac.createOMElement("fileName", omNs);
     _fileName.setText(fileName);
     OMElement _fileType = fac.createOMElement("fileType", omNs);
     _fileType.setText(fileType);
     data.addChild(_fileName);
     data.addChild(_fileType);
     data.addChild(fileContent);
     return data;
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值