问题描述
我正在使用Dropwizard 0.7.0构建用于文件上传的API.遇到麻烦,无法验证上传的文件大小限制.我想在将文件写入磁盘之前检查大小
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();
.........
}
它会引发错误:
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
升级到dropwizard 0.8.0,但遇到另一个错误
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
使用这些依赖项
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-forms</artifactId>
<version>${dropwizard.version}</version>
</dependency>
和
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.23.2</version>
</dependency>
已添加
bootstrap.addBundle(new MultiPartBundle());
(也是第一次失败之后)
and this too (after first failure)
env.jersey().register(MultiPartFeature.class);
我在这里想念什么?
推荐答案
要提交多部分数据,还需要一个额外的dropwizard依赖项.对于0.8及更高版本,依赖性为dropwizard-forms
.
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
示例用法:
@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.
如果没有,则有可能使用dropwizard 0.7,但我不必这样做...从一个快速的google看来,您需要以下依赖项:
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>
...,然后将以下内容添加到您的应用程序run
方法中:
... and add the following to your applications run
method:
environment.jersey().register(MultiPartFeature.class);
鉴于错误没有针对servlet的多部分配置",我假设您的上载在没有大小检查的情况下根本无法使用?
Given the error "No multipart config for servlet" I'm assuming your upload doesn't work at all, without your size check?
这篇关于如何查找通过Dropwizard REST API上传的文件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!