I am using Dropwizard 0.7.0 to build an API for file upload. Ran into trouble validating the uploaded file size limit. I want to check the size before writing the file to disk
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response uploadFile(@Context final HttpServletRequest request, @FormDataParam("file") FormDataBodyPart fileBodyPart) {
/*
* Check the request size
*/
request.getPart("file").getSize();
.........
}
it throws an error:
java.lang.IllegalStateException: No multipart config for servlet
at org.eclipse.jetty.server.Request.getParts(Request.java:2075) ~[jetty- server-9.0.7.v20131107.jar:9.0.7.v20131107]
at org.eclipse.jetty.server.Request.getPart(Request.java:2055) ~[jetty-server-9.0.7.v20131107.jar:9.0.7.v20131107]
EDIT ----------------------
@David
Upgraded to dropwizard 0.8.0 however ran into another error
com.sun.jersey.spi.inject.Errors: The following errors and warnings have been detected with resource and/or provider classes:
org.glassfish.jersey.media.multipart.file.FormDataBodyPart
org.glassfish.jersey.media.multipart.file.FileDataBodyPart
org.glassfish.jersey.media.multipart.FormDataContentDisposition
Using these dependencies
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-forms</artifactId>
<version>${dropwizard.version}</version>
</dependency>
and
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.23.2</version>
</dependency>
added
bootstrap.addBundle(new MultiPartBundle());
and this too (after first failure)
env.jersey().register(MultiPartFeature.class);
what am I missing here?
Being able to submit multipart data requires an additional dropwizard dependency. For 0.8 and higher the dependency is
dropwizard-forms
.http://www.dropwizard.io/0.8.0/docs/manual/forms.html
Example usage:
This is what I'm using. Perhaps upgrading is a solution for you.
If not, it is possible for dropwizard 0.7 but I haven't had to do it... From a quick google, it looks like you need the following dependency:
... and add the following to your applications
run
method:Given the error "No multipart config for servlet" I'm assuming your upload doesn't work at all, without your size check?