3

How to implement multipart/form-data request (file upload) handler with JAX-RS without vendor specific libraries? So far I haven't found other way than to inject the HttpServletRequest and use the Servlet API to access the form data.

Yet HttpServletRequest#getParts() returns an empty list even the request is well formed (confirmed with Wireshark). I read I have to enable multipart configuration for the Jersey Servlet in the web.xml. However, I'm using @ApplicationPath annotation to automatically configure JAX-RS. So what is the correct way to handle multipart requests?

Tuomas Toivonen
  • 21,690
  • 47
  • 129
  • 225

1 Answers1

2

This code may inspire you

1) JAXRS App setup

import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.media.multipart.MultiPartFeature;


@ApplicationPath("demo") 
public class ApplicationConfig extends ResourceConfig {
   public ApplicationConfig() {
     packages("com.mycompany.demo").register(MultiPartFeature.class); // <= here!
   }
}

2) JAXRS service

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadImage(
    @FormDataParam("file") InputStream data,
    @FormDataParam("file") FormDataContentDisposition fileInfo) {
...
}    
TacheDeChoco
  • 3,683
  • 1
  • 14
  • 17