title: Trying to create a web service to upload a file - Jersey with embedded Jetty
body: <p>I'm trying to create a web service using Maven. I used DarchetypeArtifactId=maven-archetype-quickstart and I have created the instances of the jetty on main class and it works </p>
  
  <pre><code>package com.rest.test;
  
  import org.eclipse.jetty.server.Server;
  import org.eclipse.jetty.servlet.ServletContextHandler;
  import org.eclipse.jetty.servlet.ServletHolder;
  
  public class App {
      public static void main(String[] args) throws Exception {
          ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
          context.setContextPath("/");
  
          Server jettyServer = new Server(8080);
          jettyServer.setHandler(context);
  
          ServletHolder jerseyServlet = context.addServlet(
               org.glassfish.jersey.servlet.ServletContainer.class, "/*");
          jerseyServlet.setInitOrder(0);
  
          // Tells the Jersey Servlet which REST service/class to load.
          jerseyServlet.setInitParameter(
             "jersey.config.server.provider.classnames",
             UploadFileService.class.getCanonicalName());
  
          try {
              jettyServer.start();`enter code here`
              jettyServer.join();
          } finally {
              jettyServer.destroy();
          }
      }
  }
  </code></pre>
  
  <p>the problem is related with the code in UploadFileService class, what class package or dependencies should I used? </p>
  
  <p>I tried with “jersey-multipart.jar” but I had a dependency conflict with Jetty.<br>
  I also tried with Servlet 3.0 API, but I couldn't get it work with embedded jetty.</p>
  
  <p>I found several examples:</p>
  
  <p><a href="https://stackoverflow.com/questions/5772225/trying-to-upload-a-file-to-a-jax-rs-jersey-server">Trying to upload a file to a JAX-RS (jersey) server</a> - great example but it is for jersey server</p>
  
  <p><a href="http://www.codejava.net/java-ee/servlet/java-file-upload-example-with-servlet-30-api" rel="nofollow noreferrer">http://www.codejava.net/java-ee/servlet/java-file-upload-example-with-servlet-30-api</a> - great example but it is for Tomcat server  </p>
  
  <p><a href="http://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-file-upload-example/" rel="nofollow noreferrer">http://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-file-upload-example/</a> - great example but it is for web application</p>
  
  <p>I really appreciate if anybody can give any help or any nitty-gritty.</p>
  
  <p>Thanks in advance!</p>
  
  <p>Note: it was marked as duplicate by peeskillet, IT IS NOT DUPLICATED! <a href="https://stackoverflow.com/questions/30653012/multipart-form-data-no-injection-source-found-for-a-parameter-of-type-public-ja">MULTIPART_FORM_DATA: No injection source found for a parameter of type public javax.ws.rs.core.Response</a> is using tomcat and my question is about embedded jetty. The only coincidence is the class name UploadFileService.class</p>
  
  <hr>
  
  <p>Hi all</p>
  
  <p>I modified the class, now I have two class JettyServer where I set the server and UploadFile where i try to manage the POST</p>
  
  <pre><code>package com.rest.test.restprj;
  
  import org.eclipse.jetty.server.Handler;
  import org.eclipse.jetty.server.Server;
  import org.eclipse.jetty.server.handler.DefaultHandler;
  import org.eclipse.jetty.server.handler.HandlerList;
  import org.eclipse.jetty.server.handler.ResourceHandler;
  import org.eclipse.jetty.servlet.ServletContextHandler;
  import org.eclipse.jetty.servlet.ServletHolder;
  import org.glassfish.jersey.server.ServerProperties;
  
  public class JettyServer
  {
  public static void main(String[] args) throws Exception
  {
  
      Server jettyServer = new Server(8080);
  
      // Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
      // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
  
      ResourceHandler resource_handler = new ResourceHandler();
  
      // Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
      // In this example it is the current directory but it can be configured to anything that the jvm has access to.
      resource_handler.setDirectoriesListed(true);
      resource_handler.setWelcomeFiles(new String[]{ "./html/index.html" });
      resource_handler.setResourceBase(".");
  
      //Jersey ServletContextHandler
  
  
      ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
      ServletHolder jerseyServlet = servletContextHandler.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
      jerseyServlet.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "com.rest.test.restprj");
  
      // Add the ResourceHandler to the server.
      HandlerList handlers = new HandlerList();
      handlers.setHandlers(new Handler[] { resource_handler, servletContextHandler, new DefaultHandler() });
      jettyServer.setHandler(handlers);
  
      try {
          jettyServer.start();
          jettyServer.join();
      } finally {
          jettyServer.destroy();
      }
  }
  }
  </code></pre>
  
  <p>Everything seems to be ok with the above class. But I got confused what i needed to register the MultiPartFeature. I got the error: </p>
  
  <p>SEVERE: Following issues have been detected: 
  WARNING: No injection source found for a parameter of type public javax.ws.rs.core.Response com.rest.test.restprj.UploadFile.postMsg(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) throws java.lang.Exception at index 0.</p>
  
  <pre><code>@Path("/fileUpload")
  public class UploadFile {
  
      @POST
      //@Consumes("multipart/form-data")
      //@Consumes("image/jpeg")
      @Produces("text/plain")
      @Consumes(MediaType.MULTIPART_FORM_DATA)
      public Response postMsg (
              @FormDataParam("file") InputStream stream,
              @FormDataParam("file") FormDataContentDisposition fileDetail) throws Exception {
  
  
          String uploadedFileLocation = "./" + fileDetail.getFileName();
  
  
          // save it
          writeToFile(stream, uploadedFileLocation);
  
          String output = "File uploaded to : " + uploadedFileLocation;
  
          final Application application = new ResourceConfig()
                  .packages("org.glassfish.jersey.examples.multipart")
                  .register(MultiPartFeature.class);
  
          return Response.status(200).entity(output).type(MediaType.TEXT_PLAIN).build();
  
  }
  
      private void writeToFile(InputStream uploadedInputStream,
              String uploadedFileLocation) {
  
              try {
                  OutputStream out = new FileOutputStream(new File(
                          uploadedFileLocation));
                  int read = 0;
                  byte[] bytes = new byte[1024];
  
                  out = new FileOutputStream(new File(uploadedFileLocation));
                  while ((read = uploadedInputStream.read(bytes)) != -1) {
                      out.write(bytes, 0, read);
                  }
                  out.flush();
                  out.close();
              } catch (IOException e) {
  
                  e.printStackTrace();
              }
  
          }
  
  } 
  </code></pre>