REST based File upload using Apache CXF and Spring

You can find the introductions about RESTFUL services here

Apache CXF is an open source implementation of JAX-WS and JAX-RS.

It originated as the combination of two open-source projects: Celtix developed by IONA Technologies (acquired by Progress Software in 2008) and XFire developed by a team hosted at Codehaus. These two projects were combined by people working together at the Apache Software Foundation. The name CXF derives from combining the “Celtix” and “XFire” project names.

Step 1:
Maven dependency for Apache CXF




Step 2:
Add spring dependency





Step 3:
web.xml configuration of both spring and Apache CXF

		<display-name>CXF Servlet</display-name>


With this configuration Apache CXF will take over all the URL which starts like //rest

Step 4:
create the REST service to handle upload

import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import javax.activation.DataHandler;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

 *  File upload service to upload files using RESTFUL services. 
 *  Author: Ananth Durai

public class FileUploadService {

    public Response uploadFile(List<Attachment> attachments,@Context HttpServletRequest request) {
        for(Attachment attr : attachments) {
            DataHandler handler = attr.getDataHandler();
            try {
                InputStream stream = handler.getInputStream();
                MultivaluedMap map = attr.getHeaders();
                OutputStream out = new FileOutputStream(new File(getFileName(map)));

                int read = 0;
                byte[] bytes = new byte[1024];
                while ((read = != -1) {
                    out.write(bytes, 0, read);
            } catch(Exception e) {

        return Response.ok("file uploaded").build();

    private String getFileName(MultivaluedMap<String, String> header) {

        String[] contentDisposition = header.getFirst("Content-Disposition").split(";");

        for (String filename : contentDisposition) {
            if ((filename.trim().startsWith("filename"))) {

                String[] name = filename.split("=");

                String finalFileName = name[1].trim().replaceAll("\"", "");
                return finalFileName;
        return "unknown";


Step 5:
Registering services as spring bean on beans.xml

 <bean id="uploadserviceclass" class="FileUploadService" />
 <jaxrs:server id="uploadservice" address="/upload">
            <ref bean="uploadserviceclass" />

Step 6:
Test jsp client to test the application

	<h1>JAX-RS Upload Form</h1>

	<form action="http://<server>/<contextpath>/rest/upload/file" method="post" enctype="multipart/form-data">

		Select a file : <input type="file" name="uploadedFile" size="50" />
	   The Next file : <input type="file" name="uploadedFile2" size="50" />

	   <input type="submit" value="Upload It" />



6 thoughts on “REST based File upload using Apache CXF and Spring

  1. how can i send base64 encoded string of an image to the restful webservice.can u help with the jax rs annotations and mime type that must be used to consume the string

    • @Override
      public void uploadPhoto(String filePath) {
      String url = “http://yourservice/serviceurl”;
      String output = null;
      PostMethod mPost = new PostMethod(url);
      HttpClient client = new HttpClient();

      try {
      File imageFile = new File(filepath);
      BufferedImage image =;
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ImageIO.write(image, “jpg”, baos);
      byte[] encodedImage = Base64.encodeBase64(baos.toByteArray());

      String data = ” ”
      + ” ”
      + “” + “image/jpeg” +” ”
      + “” + new String(encodedImage) +” ”
      + “”;

      mPost.setRequestHeader(“Content-Type”, “text/xml”);
      client.executeMethod( mPost );
      output = mPost.getResponseBodyAsString( );
      mPost.releaseConnection( );
      } catch (HttpException e) {
      // TODO Auto-generated catch block
      } catch (IOException e) {
      // TODO Auto-generated catch block

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s