Use AXIS2 transport attachment (AXIS2 MTOM)

Source: Internet
Author: User
Tags gettext tomcat

1. Working environment

Ide:eclipse 3.1.2

Jdk:jdk1.5.0_04

tomcat:apache-tomcat-5.0.28

axis2:1.0 (War version and bin version)

2. Realize

To create a new Dynamic Web project in Eclipse, add the jar packages required for Axis2 under Web-inf\lib.

This example is a system of users upload download picture format File example, every time on the outgoing carry attachments, but also include file name, file type. The 2 features implemented by this webservice are upload, download.

AXIS2 's WebService was released as a Xxx.aar release, Xxx.aar the expanded directory structure

--

--meta-inf

Services.xml

--Contains the server-side implementation Class (Directory is the same structure as package)

3. Server-Side Filetransferserver.java

Package sample;

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.File;


import Java.io.FileOutputStream;


import Java.io.InputStream;


import Java.util.Iterator;


import Javax.activation.DataHandler;


import Javax.activation.FileDataSource;


public class Filetransferserver {


public static final String Tmp_path = "D:/temp";


public omelement Upload (omelement element) throws Exception {


omelement _filecontent = null;//file content


omelement _filename = null;//filename


omelement _filetype = null;//file type


System.out.println ("The element for upload:" + element);


for (Iterator _iterator = element.getchildelements (); _iterator


. Hasnext ();) {


omelement _ele = (omelement) _iterator.next ();


if (_ele.getlocalname (). Equalsignorecase ("Filecontent")) {


_filecontent = _ele;


           }


if (_ele.getlocalname (). Equalsignorecase ("FileName")) {


_filename = _ele;


           }


if (_ele.getlocalname (). Equalsignorecase ("FileType")) {


_filetype = _ele;


           }


        }


if (_filecontent = null | | | _filetype = NULL) {


throw new Axisfault ("Either Image or FileName is null");


        }


omtext Binarynode = (omtext) _filecontent.getfirstomchild ();


String fileName = _filename.gettext ();


String fileType = _filetype.gettext ();


String storedir = Tmp_path + "/" + "temptest";


file dir = new file (Storedir);


if (!dir.exists ()) {


Dir.mkdir ();


        }


String FilePath = Storedir + "/" + FileName + "." + FileType;


file UploadFile = new file (FilePath);


if (uploadfile.exists ()) {


FilePath = Storedir + "/" + FileName + "(1)" + "." + FileType;


uploadfile = new File (FilePath);


        }


//Extracting the data and saving


DataHandler ACTUALDH;


ACTUALDH = (datahandler) binarynode.getdatahandler ();


FileOutputStream imageoutstream = new FileOutputStream (uploadfile);


InputStream is = Actualdh.getinputstream ();


Imageoutstream.write (Ioutils.getstreamasbytearray (IS));


//Setting Response


omfactory FAC = omabstractfactory.getomfactory ();


Omnamespace ns = Fac.createomnamespace ("Http://example.org/filedata",


"FD");


omelement ele = fac.createomelement ("response", NS);


Ele.settext ("true");


return ele;


    }


public omelement Download (omelement element) throws Exception {


System.out.println ("The element for download:" + Element);


omelement _username = null;


omelement _filename = null;


omelement _filetype = null;


for (Iterator _iterator = element.getchildelements (); _iterator


. Hasnext ();) {


omelement _ele = (omelement) _iterator.next ();


if (_ele.getlocalname (). Equalsignorecase ("UserName")) {


_username = _ele;


           }


if (_ele.getlocalname (). Equalsignorecase ("FileName")) {


_filename = _ele;


           }


if (_ele.getlocalname (). Equalsignorecase ("FileType")) {


_filetype = _ele;


           }


        }


String userName = _username.gettext ();


String fileName = _filename.gettext ();


String fileType = _filetype.gettext ();


String FilePath = Tmp_path + "/" + UserName + "/" + FileName + "."


+ FileType;


System.out.println ("The FilePath for download:" + FilePath);


Filedatasource dataSource = new Filedatasource (FilePath);


DataHandler EXPECTEDDH = new DataHandler (DataSource);


omfactory FAC = omabstractfactory.getomfactory ();


Omnamespace ns = Fac.createomnamespace ("Http://example.org/filedata",


"FD");


Omtext textData = Fac.createomtext (Expecteddh, true);


omelement ele = fac.createomelement ("response", NS);


Ele.addchild (TextData);


return ele;


    }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.