blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

JAX-RS: Sending multipart form data


You need to send or received data as "multipart/form-data" if it involves data like files.

@Path("/hello")
public class HelloResource {
@Path("fileupload")
 @POST
 @Consumes("multipart/form-data")
 @Produces("multipart/form-data")
 public Response postFormData(@FormParam("fileid") int theFileid,
                              @FormParam("description") String theDescription,
                              @FormParam("thefile") File theFile) {
  // echo what we got in the form
     BufferedOutMultiPart bomp = new BufferedOutMultiPart();
     OutPart op = new OutPart();
     op.setLocationHeader("thefile");
     op.setBody(theFile);
     op.setContentType(MediaType.TEXT_PLAIN);  //or other appropriate type
     bomp.addPart(op);
     
     op = new OutPart();
     op.setLocationHeader("description");
     op.setBody(theDescription);
     op.setContentType(MediaType.TEXT_PLAIN);
     bomp.addPart(op);

     op = new OutPart();
     op.setLocationHeader("fileid");
     op.setBody(theFileid + "");  //just in case theFileid is uninitialized
     op.setContentType(MediaType.TEXT_PLAIN);
     bomp.addPart(op);

     return Response.ok(bomp, "multipart/form-data").build();
 }
}

<form action="/RSServer/HelloApp/hello/fileupload" method="post"
 enctype="multipart/form-data">
 FileId: <input type="text" name="fileid" /> 
FileDesc: <input type="text" name="description" /> 
<input type="file" name="thefile" /> 
<input type="submit" name="submit" value="submit" />
</form>

The originator of the form POST submission can generate a Content-Transfer-Encoding header for one or more parts of the multipart message. The IBM JAX-RS implementation attempts to auto-decode the payload of the part according to this header when the header is of base64 or quoted-printable encoding type.
(optional) If you do not want the IBM JAX-RS implementation to auto-decode the part payload, put the @Encoded annotation on the method parameter. The following example illustrates the use of the @Encoded annotation on the method parameter:
@POST
@Consumes("multipart/form-data")
@Produces("multipart/form-data")
public Response postFormData(@FormParam("fileid") int theFileid,
                             @FormParam("description") String theDescription,
                             @Encoded @FormParam("thefile") File theFile) {
    // don't auto-decode the file part payload
    ...
}

If you want to have complete control of the retrieval and decoding of all parts in a multipart/form-data message, you can receive the BufferedInMultiPart object itself:
import org.apache.wink.common.model.multipart.BufferedInMultiPart;
import org.apache.wink.common.model.multipart.InPart;
@POST
@Consumes("multipart/form-data")
@Produces("multipart/form-data")
public Response postFormData(BufferedInMultiPart bimp) {
    List parts = bimp.getParts();
    // iterate over the "parts" and process them however you like
}

No comments:

Post a Comment