我有以下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
这是堆栈跟踪
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
如果有帮助,我正在使用Google Chrome。
最佳答案
peeskillet是正确的。我使用了不正确的依赖关系。我更新了pom.xml文件,使其包含RESTEasy jar,并且开始工作。
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
</dependency>