How to find the size of a file uploaded via Dropwi

2019-05-31 05:46发布

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?

1条回答
迷人小祖宗
2楼-- · 2019-05-31 06:34

Being able to submit multipart data requires an additional dropwizard dependency. For 0.8 and higher the dependency is dropwizard-forms.

<dependency>
    <groupId>io.dropwizard</groupId>
    <artifactId>dropwizard-forms</artifactId>
    <version>${dropwizard.version}</version>
</dependency>

http://www.dropwizard.io/0.8.0/docs/manual/forms.html

Example usage:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
        @FormDataParam("myFileName") InputStream file,
        @FormDataParam("myFileName") FormDataContentDisposition fileMetaData
){

    long fileSize = fileMetaData.getSize();
    // etc
}

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:

<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>1.18.1</version>
</dependency>

... and add the following to your applications run method:

environment.jersey().register(MultiPartFeature.class);

Given the error "No multipart config for servlet" I'm assuming your upload doesn't work at all, without your size check?

查看更多
登录 后发表回答