Search
Re: The Point of This Blog

Hi, I'm Matt Grommes and this is my blog thing. I write here about my journey to be a better programmer, things I build, and stuff I just think is interesting or fun.

I'm the founder and creator of DebugMap, a tool to help with debugging. Check it out!

Social
Blog Index
The journal that this archive was targeting has been deleted. Please update your configuration.
Navigation
« Merlin Mann: Scared Shitless | Main | How I Got My New Job »
Saturday
Feb192011

File uploads with CXF Multipart form posts

Now that the title of this post has scared off all non-technical readers of my blog (sorry Mom! :)) I wanted to come back to hopefully regular posting with a bang.

I've been working a side project that involves a REST web service. I needed to upload a file along with some identifying data and for the life of me I couldn't figure it out. I could get files to upload with a PUT or send the data with a POST, but couldn't do both in one call. I finally got it and I want to keep it here since finding full examples for CXF stuff isn't easy.

First, here's the interface for the method:


@POST
@Path("/stuff")
@Produces("application/json")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String doStuff(@Multipart("id")Integer id, @Multipart("data")String foo, @Multipart("image")byte[] image);


The @Consumes annotation with the @Multipart annotations on the parameters is the vital bit. These allows you to just grab the parameters in the implementation and not mess around with getting the Multipart Attachments and all that.


@Override
public String doStuff(Integer id, String foo, byte[] image) {
}


I'm sending the file with Android, using the HttpClient api.


HttpPost request = new HttpPost(url);

MultipartEntity mentity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayBody imgBody = new ByteArrayBody(imageBytes, "image/jpeg", "image");
mentity.addPart("image", imgBody);

//add headers
for(NameValuePair h : headers)
{
request.addHeader(h.getName(), h.getValue());
}

for(NameValuePair p : params) {
mentity.addPart(p.getName(), new StringBody(p.getValue(), "text/plain", Charset.forName( "UTF-8" )));
}

request.setEntity(mentity);

executeRequest(request, url);


This is nice and simple, and it works fine for me. Hopefully this will be helpful to you too if you're like me and Google this issue a hundred times. :)

Reader Comments (4)

Perfect ! Exactly what i needed. Simple, clear.
Thanks a billion !

(from France).

Aug 25, 2011 at 1:28 AM | Unregistered Commenterjp

Hi Matt,

The post was very good, I have a scenario , where in I have to send the image file from a html page to a cxf rest service.

Appreciate any help.

thanks,
Manohar

Oct 10, 2011 at 8:48 AM | Unregistered CommenterManohar

Thanks man! This saved of me days of trouble!

Jan 4, 2012 at 11:09 AM | Unregistered Commenterakfanta

Thanks a lot :-). It's really helpful for those who trying to upload the file to REST api's

Feb 8, 2012 at 3:19 AM | Unregistered CommenterMithun

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>