免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 808 | 回复: 0
打印 上一主题 下一主题

Web Services以及Axis2技术(文件传送的具体实现) [复制链接]

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-21 08:44 |只看该作者 |倒序浏览
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.axiom.soap.SOAP11Constants;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.InputStream;
public class FileTransferClient {
   private static EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/interop");
  
   public static boolean upload(String mailboxnum, short greetingType, File file, String fileType) {
     try {
      OMElement data = buildUploadEnvelope(mailboxnum, greetingType, file, fileType);
      Options options = buildOptions();
      ServiceClient sender = new ServiceClient();
      sender.setOptions(options);
      System.out.println(data);
      OMElement ome = sender.sendReceive(data);
      System.out.println(ome);
      String b = ome.getText();
      return Boolean.parseBoolean(b);
     }
     catch(Exception e) {
      
     }
     return false;
   }
   public static InputStream download(String mailboxnum, short greetingType, String FileType) {
     try {
       OMElement data = buildDownloadEnvelope(mailboxnum, greetingType, FileType);
       Options options = buildOptions();
       ServiceClient sender = new ServiceClient();
       sender.setOptions(options);
       System.out.println(data);
       OMElement ome = sender.sendReceive(data);
       System.out.println(ome);
       OMText binaryNode = (OMText) ome.getFirstOMChild();
       DataHandler actualDH = (DataHandler) binaryNode.getDataHandler();
       return actualDH.getInputStream();
      }
      catch(Exception e) {
      }
     return null;
   }
  
   private static OMElement buildUploadEnvelope(String mailboxnum, short greetingType, File file, String FileType) {
     DataHandler expectedDH;
     OMFactory fac = OMAbstractFactory.getOMFactory();
     OMNamespace omNs = fac.createOMNamespace("http://example.org/mtom/data", "x");
     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 mboxnum = fac.createOMElement("mailboxnum", omNs);
     mboxnum.setText(mailboxnum);
     OMElement gtType = fac.createOMElement("greetingType", omNs);
     gtType.setText(greetingType+"");
     OMElement fileType=fac.createOMElement("fileType", omNs);
     fileType.setText(FileType);
  
     data.addChild(mboxnum);
     data.addChild(gtType);
     data.addChild(fileType);
     data.addChild(fileContent);
     return data;
   }
   private static OMElement buildDownloadEnvelope(String mailboxnum, short greetingType, String FileType) {
     OMFactory fac = OMAbstractFactory.getOMFactory();
     OMNamespace omNs = fac.createOMNamespace("http://example.org/mtom/data", "x");
     OMElement data = fac.createOMElement("download", omNs);
     OMElement mboxnum = fac.createOMElement("mailboxnum", omNs);
     mboxnum.setText(mailboxnum);
     OMElement gtType = fac.createOMElement("greetingType", omNs);
     gtType.setText(greetingType+"");
     OMElement fileType=fac.createOMElement("fileType", omNs);
     fileType.setText(FileType);
     data.addChild(mboxnum);
     data.addChild(gtType);
     data.addChild(fileType);
     return data;
   }
   private static Options buildOptions() {
     Options options = new Options();
     options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
     options.setTo(targetEPR);
     // enabling MTOM in the client side
     options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
    options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
     return options;
   }
   public static void main(String agrs[]) {    //此为测试主函数
     String file = "C:/deploy.wsdd";
     short gt = 1;
     String mn = "20060405";
     String ft="wsdd";
     boolean rtv = upload(mn,gt,new File(file),ft);
     System.out.println(rtv);
     InputStream is = download(mn,gt,ft);
   }
}

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP