Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey Multipart - Missing start boundary

I have a server application with jersy 2.13 with Netty and I try to upload a file with "multipart/form-data" and I got this error.

The Error Message:

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'

My dependencies

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"

My resource :

@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);
    }
}

My Application:

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

    register(new InjectionBinder());
    register(new MultiPartFeature());
    register(new MyFileUploader());
}

My client test

<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>

If I used jersey 1.8 (compile group: "com.sun.jersey.contribs", name: "jersey-multipart", version: "1.18.3"), it's working if I remove the FormDataContentDisposition from the function uploadFile. If I don't remove it i have this error on the startup:

    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)

If I remove the FormDataContentDisposition from the uploadFile function my file content is the following:

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

my file content
------WebKitFormBoundaryATOqpm55xXBvTACH--
like image 727
plemay Avatar asked Dec 10 '14 14:12

plemay


1 Answers

I ran into a similar issue with Jersey where multipart file uploads would fail when using Chrome, but succeed with Firefox.

The issue, as plemay alluded to, is that Chrome sends a boundary in the Content-Type header that contains upper and lower case letters, but something on the server side is converting it all to lowercase, causing parsing of the body to fail with "MIMEParsingException: Missing start boundary."

The cause in my case was that Jersey uses SPI to load classes at Runtime, and it was picking up a buggy MediaType implementation from Apache CXF that converted header values to lowercase. The fix is to upgrade/get rid of CXF, or force Jersey to use a different MediaType implementation.

See JERSEY-1377 for more details.

like image 121
Patrick Avatar answered Sep 26 '22 20:09

Patrick