Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5437964
  • 博文数量: 895
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8691
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(895)

文章存档

2021年(2)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: 系统运维

2011-05-12 23:02:08

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;
import org.apache.axiom.om.OMText;
import org.apache.axis2.AxisFault;
import java.io.FileOutputStream;
import java.io.*;
import java.util.Iterator;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
public class interopService {
public static final String TMP_PATH = "c:/tmp";
public OMElement upload(OMElement element) throws Exception {
    OMElement _fileContent = null;
    OMElement _mailboxnum = null;
    OMElement _greetingType = null;
    OMElement _fileType = null;
    System.out.println(element);
    for (Iterator _iterator = element.getChildElements(); _iterator.hasNext();) {
      OMElement _ele = (OMElement) _iterator.next();
      if (_ele.getLocalName().equalsIgnoreCase("fileContent")) {
        _fileContent = _ele;
      }
      if (_ele.getLocalName().equalsIgnoreCase("mailboxnum")) {
        _mailboxnum = _ele;
      }
      if (_ele.getLocalName().equalsIgnoreCase("greetingType")) {
        _greetingType = _ele;
      }
      if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
        _fileType = _ele;
      }
    }
    if (_fileContent == null || _mailboxnum == null || _greetingType== null || _fileType==null) {
      throw new AxisFault("Either Image or FileName is null");
    }
    OMText binaryNode = (OMText) _fileContent.getFirstOMChild();
    String mboxNum = _mailboxnum.getText();
    String greetingType = _greetingType.getText();
    String fileType = _fileType.getText();
    String greetingstoreDir = TMP_PATH+"/"+mboxNum;
    File dir = new File(greetingstoreDir);
    if(!dir.exists()) {
      dir.mkdir();
    }
    String filePath = greetingstoreDir+"/"+greetingType+"."+fileType;
    File greetingFile = new File(filePath);
    if(greetingFile.exists()) {
      greetingFile.delete();
       greetingFile = new File(filePath);
    }
   
    // Extracting the data and saving
    DataHandler actualDH;
    actualDH = (DataHandler) binaryNode.getDataHandler();
    
    FileOutputStream imageOutStream = new FileOutputStream(greetingFile);
    InputStream is = actualDH.getInputStream();
    imageOutStream.write(IOUtils.getStreamAsByteArray(is));
    // setting response
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace ns = fac.createOMNamespace("", "x");
    OMElement ele = fac.createOMElement("response", ns);
    ele.setText("true");
   
    return ele;
}
public OMElement download(OMElement element) throws Exception {
    System.out.println(element);
    OMElement _mailboxnum = null;
    OMElement _greetingType = null;
    OMElement _fileType = null;
    for (Iterator _iterator = element.getChildElements(); _iterator.hasNext();) {
      OMElement _ele = (OMElement) _iterator.next();
      if (_ele.getLocalName().equalsIgnoreCase("mailboxnum")) {
        _mailboxnum = _ele;
      }
      if (_ele.getLocalName().equalsIgnoreCase("greetingType")) {
        _greetingType = _ele;
      }
      if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
        _fileType = _ele;
      }
    }
    String mboxNum = _mailboxnum.getText();
    String greetingType = _greetingType.getText();
    String fileType = _fileType.getText();
    String filePath = TMP_PATH+"/"+mboxNum+"/"+greetingType+"."+fileType;
    FileDataSource dataSource = new FileDataSource(filePath);
    DataHandler expectedDH = new DataHandler(dataSource);
   
    OMFactory fac = OMAbstractFactory.getOMFactory();
   
    OMNamespace ns = fac.createOMNamespace("", "x");
    OMText textData = fac.createOMText(expectedDH, true);
    OMElement ele = fac.createOMElement("response", ns);
    ele.addChild(textData);
    return ele;
}
}
Services.xml配置文件:
name="MTOMService">
   
        This is a sample Web Service with two operations,echo and ping.
   
    name="ServiceClass"locked="false">sample.mtom.interop.service.interopService
    name="upload">
        urn:upload
        class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
   
      name="download">
        urn:download
        class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
   

阅读(373) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~