0
我有以下Web服務,它應該通過HTML表單獲取文件,將它保存在服務器上的某個地方。javax.ws.rs.NotSupportedException:找不到類型的消息正文閱讀器:class com.sun.jersey.core.header.FormDataContentDisposition
@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」),但我得到相同的錯誤。
瀏覽器發送的HTTP報文,以下內容類型
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryHvAymGjNVVEB1VWa
我從服務器獲取
415 Unsupported Media Type
一個415的響應。如果有幫助,我使用谷歌瀏覽器。
從stacktrace,你似乎使用RESTeasy,而不是澤西島。請參閱[RESTeasy文檔](http://docs.jboss.org/resteasy/docs/3.0.12.Final/userguide/html/Multipart.html)。這是您需要的[依賴項](http://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-multipart-provider) –
拋出錯誤的FormDataContentDisposition類來自jersey-contrib。 – thisisshantzz
這不是澤西類拋出異常。它的RESTeasy告訴你它不支持Jersey類型。查看我鏈接到的多部分支持的文檔。您將不會使用Jersey組件。你可以完全擺脫澤西島的依賴 –