Thursday, October 3, 2019

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










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.(ApplicationHandler.java:320)
at org.glassfish.jersey.server.ApplicationHandler.(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--

Answer



The problem is in the boundary received from the browser.



Chrome Boundary: boundary=----webkitformboundary2fvqbgcbynvtvptx (Not working)



Firefox Boundary: boundary=---------------------------13335242989826 (Working)


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...