我有以下Web服务,它应该通过HTML表单获取文件并将其保存在服务器上的某个位置。
@Path("/dv")
public class Uploader {
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream stream, @FormDataParam("file") FormDataContentDisposition fileDetails) {
FileOperations fo = new FileOperations(stream); // My own class
try {
fo.saveToFile(fileDetails.getFileName());
} catch (IOException ex) {
ex.printStackTrace();
}
return Response.status(200).entity("").build();
}
}
HTML表单是这样的。
<form name="upload-form" method="post" action="http://localhost:18080/dv/upload" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" />
</form>
每当我通过此表单上传文件时,都会收到错误
javax.ws.rs.NotSupportedException: Could not find message body reader for type: class com.sun.jersey.core.header.FormDataContentDisposition of content type: multipart/form-data;boundary=----WebKitFormBoundaryHvAymGjNVVEB1VWa
这是stacktrace
javax.ws.rs.NotSupportedException: Could not find message body reader for type: class com.sun.jersey.core.header.FormDataContentDisposition of content type: multipart/form-data;boundary=----WebKitFormBoundaryHvAymGjNVVEB1VWa
at org.jboss.resteasy.core.interception.ServerReaderInterceptorContext.throwReaderNotFound(ServerReaderInterceptorContext.java:52)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:73)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:50)
at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.aroundReadFrom(GZIPDecodingInterceptor.java:59)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:53)
at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:150)
at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:88)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:111)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:280)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:234)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:221)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)
at org.jboss.resteasy.core.SynchronousDispatcher.invokePropagateNotFound(SynchronousDispatcher.java:217)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:224)
at org.jboss.resteasy.plugins.server.servlet.FilterDispatcher.doFilter(FilterDispatcher.java:62)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.marmotta.platform.core.servlet.MarmottaResourceFilter$LMFFilterChain.doFilter(MarmottaResourceFilter.java:189)
at org.apache.marmotta.platform.core.filters.ModuleResourceFilter.doFilter(ModuleResourceFilter.java:169)
问题是我不明白这里有什么问题。我已将@Consumes(MediaType.MULTIPART_FORM_DATA)更改为@Consumes(“multipart / form-data”)但我得到了同样的错误。
浏览器使用以下Content-Type
发送HTTP数据包Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryHvAymGjNVVEB1VWa
我从服务器得到415响应
415 Unsupported Media Type
如果有帮助,我使用的是谷歌浏览器。
答案 0 :(得分:2)
peeskillet是正确的。我使用的是不正确的依赖项。我更新了我的pom.xml文件以包含RESTEasy jar并开始工作。
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
</dependency>