设为首页 加入收藏

TOP

通向架构师的道路(第十二天)之Axis2 Web Service(三)(六)
2018-02-22 14:32:42 】 浏览:771
Tags:通向 架构 师的 道路 十二 Axis2 Web Service
tion e) { throw new Exception(e); } finally { try { if (fileOutputStream != null) { fileOutputStream.close(); fileOutputStream = null; } } catch (Exception e) { } } return "File saved succesfully."; } }

下面是服务端的描述

 

service.xml文件的内容为:

<service name="AttachmentService">

         <parameter name="ServiceClass">org.sky.axis2.attachment.FileUploadService

         </parameter>

         <operation name="uploadFile">

                   <actionMapping>urn:uploadFile</actionMapping>

                   <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />

         </operation>

</service>

该服务端接受客户端上传的附件后使用UUID重新命名上传的文件名,并将其存入服务端的” D:/upload/axis2/”目录中。

来看客户端代码

org.sky.axis2.attachment.FileUploadClient

package org.sky.axis2.attachment;

 

import java.io.File;

 

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.xml.namespace.QName;

 

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMNamespace;

import org.apache.axiom.soap.SOAP11Constants;

import org.apache.axiom.soap.SOAPBody;

import org.apache.axiom.soap.SOAPEnvelope;

import org.apache.axiom.soap.SOAPFactory;

import org.apache.axis2.Constants;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.OperationClient;

import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

import org.apache.axis2.context.ConfigurationContext;

import org.apache.axis2.context.ConfigurationContextFactory;

import org.apache.axis2.context.MessageContext;

import org.apache.axis2.wsdl.WSDLConstants;

 

public class FileUploadClient {

         private static EndpointReference targetEPR = new EndpointReference(

                            "http://localhost:8080/Axis2Service/services/AttachmentService");

 

         public static void main(String[] args) throws Exception {

                   new FileUploadClient().transferFile();

         }

 

         public void transferFile() throws Exception {

                   String filePath = "D:/deployment/test.jpg";

                   String destFile = "test.jpg";

                   Options options = new Options();

                   options.setTo(targetEPR);

                   options.setProperty(Constants.Configuration.ENABLE_SWA,

                                     Constants.VALUE_TRUE);

                   options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);

                   options.setTimeOutInMilliSeconds(10000);

                   options.setTo(targetEPR);

                   options.setAction("urn:uploadFile");

                   ConfigurationContext configContext = ConfigurationContextFactory

                                     .createConfigurationContextFromFileSystem(

                                                        "D:/wspace/Axis2Service/WebContent/WEB-INF/modules",

                                                        null);

 

                   ServiceClient sender = new ServiceClient(configContext, null);

                   sender.setOptions(options);

                   OperationClient mepClient = sender

                                     .createClient(ServiceClient.ANON_OUT_IN_OP);

 

                   MessageContext mc = new MessageContext();

                   FileDataSource fileDataSource = new FileDataSource(new File(filePath));

 

                   //
首页 上一页 3 4 5 6 7 下一页 尾页 6/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java 开发 .gitignore 文件包含 ... 下一篇Java 非阻塞 IO 和异步 IO

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目