Implementation of WebService transfer file (AXIS2 MTOM)
Last Update:2018-07-25
Source: Internet
Author: User
Working Environment : Ide: eclipse 3.1.2 jdk:jdk1.5.0_06 tomcat:apache-tomcat-5.5.15 axis2:1.0 ( War version and bin version) Environment preparation: http://ws.apache.org/axis2/download/1_0/download.cgi to download AXIS2 Binary distribution Url:http ://apache.justdn.org/ws/axis2/1_0/axis2-std-1.0-bin.zip and War distribution URL:HTTP://APACHE.JUSTDN.ORG/WS/AXIS2/1 _0/axis2-1.0-docs.zip. Unzip the two files, such as C:/axis2-std-1.0-bin and C:/axis2.war, after extracting the extracted directory. Under Eclipse, create a new User library via the menu Window-preferences...--java-build Path-user libraries, such as the name Axis2 C:/axis2-std-1.0-bin All jar files under/lib are included in. Copy the Axis2.war to the%tomcat-home%/webapps below.            &NBsp;
Implement : Create a new project in Eclipse, install Eclipse Tomcat plugin A new Tomcat project well, build path contains the user Library:axis2 created above. My example scenario is a voicemail system for users to upload download greeting language pieces (greeting), each voicemail system user has a unique mailbox number, greeting has different types, such as busy time greeting, travel time greeting, different periods of greeting, The greeting file supports types that are Wav,mp3 and so on. So my webservice to achieve 2 functions is upload, download. AXIS2 's WebService was released as a Xxx.aar release, Xxx.aar the expanded directory structure to----Meta-inf services.xml--Contains the server-side implementation of C Lass (Directory with package is the same structure) new 2 classes: Filetransferclient.java, Interopservice.java. Two categories of content are as follows:
Filetransferclient (
Call
WebService
the client code
) Package sample.mtom.interop.client; 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); &nb sp; 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) { & nbsp; } return false; } public static InputStream download (string mailboxnum, short Greetingtype, string FileType) {& nbsp; 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) { & nbsp 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.WSD D "; 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); }}
Interopservice (WebService
of the
Server
End Implementation Code
) Package sample.mtom.interop.service; 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 (O Melement Element) throws Exception { omelement _filecontent = null; omelement _MAILB Oxnum = null; omelement _greetingtype = null; omelement _filetype = null; System.out.println (Element); for (Iterator _iterator = element.getchildelements (); _iterator.hasnext ();) {&Nbsp; 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 newAxisfault ("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 (); } & nbsp String FilePath = greetingstoredir+ "/" +greetingtype+ "." +filetype; file Greetingfile = new file (FilePath);