Upload a File with Jersey using AJAX and HTML5 progress bar

You may also like...

  • I have a similar project but somehow when I deploy it I get an error. Have you encountered anything similar?

    Severe: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: 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 javax.ws.rs.core.Response rover7c.rest.SegmentController.postSegment(java.lang.String,java.lang.String,java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 2.; source=’ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class rover7c.rest.SegmentController, handlerConstructors=[[email protected]]}, definitionMethod=public javax.ws.rs.core.Response rover7c.rest.SegmentController.postSegment(java.lang.String,java.lang.String,java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), parameters=[Parameter [type=class java.lang.String, source=userId, defaultValue=null], Parameter [type=class java.lang.String, source=deviceId, defaultValue=null], Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=file, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}’, [WARNING] The (sub)resource method postSegment in rover7c.rest.SegmentController contains empty path annotation.; source=’public javax.ws.rs.core.Response rover7c.rest.SegmentController.postSegment(java.lang.String,java.lang.String,java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition)’, [WARNING] The (sub)resource method getSegments in rover7c.rest.SegmentController contains empty path annotation.; source=’public java.lang.String[] rover7c.rest.SegmentController.getSegments(java.lang.String,java.lang.String)’]

    • Dear Mike,

      Have you based your code entirely on this tutorial?
      It sounds like you are missing a configuration in your jersey application.
      You need to register the MultiPartFeature in your jersey application configuration.
      See section ‘Registering Jersey RestServices’
      Hope this helps!

  • Hi! I am using configuration in a slightly different way. I believe both are valid, do you think this is what’s causing the problem?

    <servlet>
    <servlet-name>rest</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>rover7c.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

    • Hi Mike,

      You can configure the MultiPartFeature by extending javax.ws.rs.core.Application class and registering it by overriding the getClasses method (like I did in this application).

      Or you can configure the MultiPartFeature in your web.xml. You must add the following:

      <init-param>
      <param-name>jersey.config.server.provider.classnames</param-name>
      <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
      </init-param>

      Hope it helps !

  • omar

    Hey i was able to build youre uploader using mvn clean install and it was successful but i dont understand what modification i need to make to be able to post file to the rest service everytime i upload a file it says error. Any help would be great my project is due very soon and this would be awesome to include.

    THanks!

    • Hello Omar, Is the error originating from the server, or the client? And can you please provide more detail about which error you’re receiving?

  • Paco Gamero

    Hi!, I am testing some examples with the same bad result, your code is the last one. My problem is that the event progress is triggered only one time. For example, following your code, the event is triggered at the beginning and the progress bar is filled with the 100% value, but the file is not uploaded yet. Any idea? Thanks in advance.