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));
//