我有一个带有Netty的jersy 2.13的服务器应用程序,我尝试使用" multipart / form-data"上传文件。我收到了这个错误。
错误讯息:
7605 10:01:49.309 [child-group-3-1] org.jvnet.mimepull.MIMEParsingException: Missing start boundary
66242 08:57:42.713 [child-group-3-1] ERROR ROOT - No codec available to display error for 'Content-Type:multipart/form-data; boundary=----webkitformboundaryv4kegleyi4tkjp8j'
我的依赖
compile group: "org.glassfish.jersey.core", name: "jersey-server", version: "2.13"
compile group: "org.glassfish.jersey.media", name: "jersey-media-json-jackson", version: "2.13"
compile group: "org.glassfish.jersey.media", name: "jersey-media-multipart", version: "2.13"
我的资源:
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadFile(@FormDataParam("file") InputStream stream, @FormDataParam("file") FormDataContentDisposition contentDispositionHeader)
{
System.out.println("Enter uploadFile");
String outputPath = "C:/upload/";
java.nio.file.Path outPath = FileSystems.getDefault().getPath(outputPath, contentDispositionHeader.getFileName());
try
{
Files.copy(stream, outPath);
}
catch (IOException e)
{
throw Throwables.propagate(e);
}
}
我的申请:
public JerseyApplication()
{
super(JacksonMapper.class, JacksonFeature.class);
register(new InjectionBinder());
register(new MultiPartFeature());
register(new MyFileUploader());
}
我的客户端测试
<form action="http://localhost/api/upload" method="post" enctype="multipart/form-data">
<p><input id="uploadInput" type="file" name="file"></p>
<p><input type="submit" formenctype="multipart/form-data" value="Send file"></p>
</form>
如果我使用了jersey 1.8(compile group: "com.sun.jersey.contribs", name: "jersey-multipart", version: "1.18.3"
),那么如果我从函数FormDataContentDisposition
中移除uploadFile
,它就会正常工作。如果我不删除它,我在启动时会出现此错误:
WARNING: No injection source found for a parameter of type public void com.fs.ss.communication.jersey.FileUploader.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at index 0.
1621 09:39:10.974 [main] ERROR ROOT - Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public void com.fs.ss.communication.jersey.FileUploader.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.fs.ss.communication.jersey.FileUploader, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@599545b6]}, definitionMethod=public void com.fs.ss.communication.jersey.FileUploader.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class com.sun.jersey.core.header.FormDataContentDisposition, source=file, defaultValue=null]], responseType=void}, nameBindings=[]}']
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public void com.fs.ss.communication.jersey.FileUploader.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.fs.ss.communication.jersey.FileUploader, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@599545b6]}, definitionMethod=public void com.fs.ss.communication.jersey.FileUploader.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class com.sun.jersey.core.header.FormDataContentDisposition, source=file, defaultValue=null]], responseType=void}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:467)
at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:163)
at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:323)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:320)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:273)
at com.fs.ss.communication.protocol.http.HttpJerseyServerHandler.register(HttpJerseyServerHandler.java:195)
at com.fs.ss.communication.protocol.http.HttpServerInitializer.setJerseyResources(HttpServerInitializer.java:167)
at com.fs.ss.communication.CommunicationService.addServer(CommunicationService.java:309)
at com.fs.ss.communication.CommunicationService.initialize(CommunicationService.java:383)
at com.fs.ss.communication.CommunicationService.startApp(CommunicationService.java:399)
at com.fs.ss.communication.app.AbstractApplication.start(AbstractApplication.java:147)
at com.fs.ss.communication.CommunicationServiceBootstrap.main(CommunicationServiceBootstrap.java:40)
如果我从FormDataContentDisposition
功能中移除uploadFile
,我的文件内容如下:
------WebKitFormBoundaryATOqpm55xXBvTACH
Content-Disposition: form-data; name="file"; filename="mytxt.txt"
Content-Type: text/plain
my file content
------WebKitFormBoundaryATOqpm55xXBvTACH--
答案 0 :(得分:6)
我遇到类似的问题,在使用Chrome时,多部分文件上传失败,但使用Firefox取得了成功。
正如所提到的那样,问题是Chrome在Content-Type标头中发送包含大写和小写字母的边界,但服务器端的某些内容将其全部转换为小写,导致正文解析为&#34; MIMEParsingException失败:缺少起始边界。&#34;
我的案例中的原因是Jersey使用SPI在运行时加载类,并且它从Apache CXF中获取了一个错误的MediaType实现,它将头值转换为小写。解决方法是升级/删除CXF,或强制Jersey使用不同的MediaType实现。
有关详细信息,请参阅JERSEY-1377。
答案 1 :(得分:1)
问题在于从浏览器收到的边界。
Chrome界限:boundary=----webkitformboundary2fvqbgcbynvtvptx
(无效)
Firefox边界:boundary=---------------------------13335242989826
(工作)
答案 2 :(得分:0)
我花了大约8个小时来调试....然后我发现了一个简单的事实:
边界应该在内容类型中定义,如下所示:
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
我希望有人能告诉我这个以节省时间......所以希望它对那些像我一样迷失的人有所帮助。
我发现了另一篇帖子解决了这个问题:What is the boundary in multipart/form-data?
答案 3 :(得分:0)
org.jvnet.mimepull.MIMEParsingException:缺少开始边界:
即使我们拥有适当的Content-Type:multipart / form-data; boundary = ---- webkitformboundary2fvqbgcbynvtvptx出现异常。
解决方案是:检查Web过滤器是否还具有其他任何spring multiform-data过滤器。通过评论春季多部分过滤器进行测试,然后尝试进行平针织剩余上传,然后流量将按预期工作。
注意:请确保,如果您同时需要两个过滤器,请更改url-pattern以控制同一应用程序中的URL。
我的情况是:我评论了org.springframework.web.multipart.support.MultipartFilter,然后按预期工作正常。
谢谢 拉朱·萨马拉(Raju Samala)