Upload file to Rest service using Spring Jersey

You need to create a Rest service as described in my previous blog


@Path("upload/")
@Produces({"application/xml"})
@Component
@Scope("request")
public class Upload
{
  @POST
   //to tell that it will consume the multipart requests only
   @Consumes("multipart/form-data")  

   public String uploadFile(
      @Context HttpServletRequest request,
      @FormParam("attachmentFile") FormDataContentDisposition dispostion,
      @FormParam("attachmentFile") InputStream attachmentFile)
   {
       //file name can be retrieved from dispostion in the following manner
       String fileName = dispostion.getFileName();
       //file data can be read from the attachmentFile input stream

      //returning the response xml which can be
     // anything what you wanna show to your client
      return "ok";
   }

For client could be any html with a file tag



<form action="upload" enctype="multipart/form-data" method="POST">

//here we are giving the file name which will be 
//used in service to access the file
<input type="file" name="attachmentFile" />
<input type="submit" value="submit" />
</form>

About mukeshbansiwal

Technical Lead in a Software company. Expertise in Spring framework, Hibernate, Struts2 , Rest Api using Jersey Spring, Core Java, Google Web Tool Kit(GWT), EJB 2.0 , CSS, MySql, JavaScript, Oracle 8i, J2EE Architecture, Tomcat, Apache Web Server, Jboss, Weblogic Server 8.1
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

1 Response to Upload file to Rest service using Spring Jersey

  1. Thank you a lot!
    It has been very interesting for me.

    I will follow your blog.

Leave a comment