Jersey Multipart - 缺少起始边界

ple*_*may 5 multipart jersey jersey-2.0

我有一个带有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'
Run Code Online (Sandbox Code Playgroud)

我的依赖

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"
Run Code Online (Sandbox Code Playgroud)

我的资源:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的应用程序:

public JerseyApplication()
{
    super(JacksonMapper.class, JacksonFeature.class);

    register(new InjectionBinder());
    register(new MultiPartFeature());
    register(new MyFileUploader());
}
Run Code Online (Sandbox Code Playgroud)

我的客户测试

<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>
Run Code Online (Sandbox Code Playgroud)

如果我使用了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)
Run Code Online (Sandbox Code Playgroud)

如果我FormDataContentDispositionuploadFile函数中删除了我的文件内容如下:

------WebKitFormBoundaryATOqpm55xXBvTACH
Content-Disposition: form-data; name="file"; filename="mytxt.txt"
Content-Type: text/plain

my file content
------WebKitFormBoundaryATOqpm55xXBvTACH--
Run Code Online (Sandbox Code Playgroud)

小智 7

我在使用Chrome时遇到类似的问题,其中多部分文件上传失败,但使用Firefox成功.

正如所提到的那样,问题是Chrome在Content-Type标题中发送包含大写和小写字母的边界,但服务器端的某些内容将其全部转换为小写,从而导致正文解析失败" MIMEParsingException:缺少起始边界."

在我的情况的原因是,泽西使用SPI在运行时加载的类,它被拾起来自Apache的CXF一个越野车的MediaType实现,转换头值小写.解决方法是升级/删除CXF,或强制Jersey使用不同的MediaType实现.

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


ple*_*may 2

问题出在从浏览器接收的边界中。

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

Firefox 边界:(boundary=---------------------------13335242989826工作中)