Upload attachment (document) using CXF ( MTOM ) Spring and Tomcat

Incase you have any difficulty in setup please contact us.

Let us now create a resume upload web service using Apache CXF, Spring and Tomcat and consume it. The web service will be used to upload an attachment [resume]. The scenario considered here is a candidate uploading his/her resume and the resume [word document] is retrieved and stored on the server.

The following files are involved in the demo of the tutorial. Download the files (zip format)
cxfattachfiles (4K) This DTO (value object) represents the resume of a candidate. This interface has uploadResume method which accepts a Resume. Implementation class for Code which consumes the web service.
cxf.xml Informs Spring [which is referenced from CXF] about the related Java classes for a given web service.

Before getting into the details of the above files let us setup a web application in Tomcat 5.5 or Tomcat 6 to deploy the resume upload web service.
Step 1: Go to TOMCAT_HOME\webapps folder [In my machine it is C:\tomcat\webapps]
Step 2: Create a new folder and name it as resumeupload
Step 3: Create a folder by the name WEB-INF under resumeupload
Step 4: Create two folders classes and lib under WEB-INF.
Step 5: Download web.xml [right click and save link as]and save it under WEB-INF folder.Now your folder structure should look as in the screen shot below.
folderstruct (2K)
Step 6: Download CXF in zip format and unzip it into any folder. Copy the jar files under CXF lib folder to the lib folder under Tomcat (created in Step 4). Though all jar files are not necessary, we will make use of them in our future CXF tutorials.
Step 7: Now setup a Java project in Eclipse [with JDK 1.5 plus ] so that we can populate the classes folder created in Step 4. Download and copy the files under source directory of the Java project as in the screen shot below.
cxfattachfiles (4K)
Step 8: To resolve compilation errors add the jars under lib folder of CXF [downloaded in Step 6] to Java Build Path/Libraries of the Eclipse project.
Step 9: Build the Eclipse project and copy the classes [the cxf.xml also will be placed by eclipse along with the classes if you have selected separate folders for source and classes when setting up the eclipse Java project] under the classes folder created in Step 4. [You may exclude Client.class as it is not required under Tomcat]
Step 10: Restart the Tomcat. The deployment of the web service on Tomcat server is complete. Before we invoke the client side code [which is a breeze] let us understand the code in the files involved.
resumejava (3K) consists of getters and setters for name of the candidate, resume type [if it is doc or pdf], and the resume [attachment].
resumeserv (4K) declares the uploadResume method with the annotations. The @WebParam annotation though optional is required for a readable WSDL file [if not specified the parameter names will be as arg0, arg1 and not be meaningful....]. is the server side implementation of the web service.
resumeservimpl (9K)
It retrieves the InputStream from the DataHandler and stores the resume in C drive with the c candidateName.extensionType. The related annotations are also provided.

cxf.xml has the configuration which will be loaded by Spring and used by CXF about the web service. We have mentioned jaxws:properties which enables mtom capability of CXF where in the resume is sent as an attachment giving better performance [by avoiding base 64 encoding].
cxfxml (8K) populates values for candidate name, file attachment type, and the resume [which is path to a file] and invokes the web service.
clientjava (10K)

Finally about the web.xml which we downloaded and placed under Tomcat WEB-INF folder. This configures CXF Servlet as well as the location of cxf.xml which is to be loaded by Spring.
webxml (8K)

Now we will go back to our Eclipse project and run as a Java application. [Please change the values of server port [8080 in this example], resume location, resume file type and candidate name in as required.]. You should be able to see the file getting stored under the location specified in [in this example C drive] with the candidateName.extensionType.

This tutorial is suitable only for small attachments. To learn how to upload large attachments please refer Upload big attachment (document) using CXF ( MTOM )

Here let us tweak the code provided in the above tutorial to enable MTOM streaming capability at the client [client to the web service].

The modified section in is as below.

Map props = new HashMap();
props.put("mtom-enabled", Boolean.TRUE);

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());

Here we create a map which enables MTOM and set in the JaxWsproxyFactoryBean. [Hence the client will not try to include the entire file in the payload].


Karthikeyan C said…
I am the author of the tutorial at TheaSolutions. I would be satisfied if you post a link to the tutorial rather than copying the content in your blog.

Or atleast mention it is from

The link is
Suraj Chhetry said…
link is not working can you give me link so that i can download source.
Anonymous said…
Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!

Popular posts from this blog

Solr Cloud 4.5

PostgreSQL bytea and oid

Microservices Architecture with Spring Boot in 15mins