杰西多部分 - 缺少起始边界

5

我有一个使用jersy 2.13和Netty的服务器应用程序,我尝试使用“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"),如果我从函数uploadFile中删除FormDataContentDisposition,它可以正常工作。如果我不删除它,则会在启动时出现以下错误:

    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,那么我的文件内容如下:
------WebKitFormBoundaryATOqpm55xXBvTACH
Content-Disposition: form-data; name="file"; filename="mytxt.txt"
Content-Type: text/plain

my file content
------WebKitFormBoundaryATOqpm55xXBvTACH--
4个回答

8

我曾遇到一个类似的问题,使用Chrome上传多部分文件时失败,但使用Firefox则成功。

问题是,如plemay所提到的,Chrome在Content-Type头中发送了一个包含大写和小写字母的分界符,但服务器端的某些内容将其全部转换为小写,导致解析主体时出现“MIMEParsingException: Missing start boundary”错误。

在我的情况下,原因是Jersey在运行时使用SPI加载类,并从Apache CXF中获取了有缺陷的MediaType实现,将头值转换为小写。解决方法是升级/摆脱CXF,或强制Jersey使用不同的MediaType实现。

有关详细信息,请参见JERSEY-1377


太棒了!这让我省去了很多挫败感,谢谢Patrick! - Liv
链接重定向到关闭的网站。java.net - Jeremiah Adams

2

问题出在浏览器发送的边界上。

Chrome边界:boundary=----webkitformboundary2fvqbgcbynvtvptx(不可用)

Firefox边界:boundary=---------------------------13335242989826(可用)


你好,我看到IE浏览器中出现了缺失开始边界的问题,其中边界如下所示。但是在Chrome浏览器中边界运行正常。有什么解决方法吗?我正在使用最新的mimepull.jar文件。 Content-Type multipart/form-data; boundary=---------------------------7e1471e600ffe - jessi

0

我花了大约8个小时来调试……然后我发现了一个简单的事实:

boundary应该在content-type中定义,如下所示:

Content-Type: multipart/form-data; boundary=----webkitformboundary2fvqbgcbynvtvptx

我希望有人能告诉我这个,以节省时间……所以希望对其他像我一样迷失的人有所帮助。

我发现另一篇帖子解决了这个问题:multipart/form-data中的边界是什么?


0

org.jvnet.mimepull.MIMEParsingException: 缺少起始边界:

即使我们有正确的Content-Type:multipart/form-data; boundary=----webkitformboundary2fvqbgcbynvtvptx,仍然会出现异常。

解决方案是:检查Web过滤器是否有其他Spring多重数据过滤器。通过注释Spring多部分过滤器进行测试,然后尝试使用Jersey REST上传,然后流程将按预期工作。

注意:确保如果您需要两个过滤器,则更改url-pattern以控制同一应用程序中的URL。

在我的情况下:我注释了org.springframework.web.multipart.support.MultipartFilter,然后它按预期正常工作。

谢谢 Raju Samala


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接